python3

python3 でコンソールからの入力をリストに格納する方法

投稿日:2020年11月28日 更新日:

入力を受け取るには、input() を使う。
range(0,5) で、5個の文字列を順に受け取り、リストに入れてそれを表示する。

list1 = []   
for i in range(0, 5): 
    element = str(input(str(i) + '番目を入力: ')) 
    list1.append(element)      
print(list1) 

結果

0番目を入力: hello
1番目を入力: world
2番目を入力: good
3番目を入力: morning
4番目を入力: Taro
['hello', 'world', 'good', 'morning', 'Taro']

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib でグラフ表示ウィンドウの画面上の位置を自由に設定する方法

matplotlib.use(‘TkAgg’) としておき、 get_current_fig_manager().window.wm_geometry(“+20+50”) として、(+20+50)のと …

no image

python3 でグローバル変数とローカル変数で同じ名前の変数を使う

グローバル変数とローカル変数で同じ名前の変数を使うことができる。 例 x = 10 def myfunc(): x = 20 print(‘関数内: x = ‘+str(x)) myfunc() pr …

no image

python3 で整数の割り算、商と余りを求める方法

floor divisionを使う。 例 a = 15 // 7 b = 15 % 7 print(a) print(b) 結果 2 1

no image

python3 でランダムな整数の配列を作る方法

random モジュールを使う。randint() の引数で、配列の最小値と最大値を指定できる。 例 import random random_arr = [random.randint(-2 …

no image

matplotlib でグラフの背景の色を変える方法(facecolor)

次のように、axes で facecolor を変更すればよい。 例 from matplotlib import pyplot as plt x = [2,7,8] y = [7,1 …