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

python3 で、配列の最大値のインデックスを1つ求める方法

配列の index() メソッドを使うと、その値のインデックスを求めることができる。 最大値を求めるメソッド max と組み合わせて使う。 例 arr = [2,4,5,10,8,-3] in …

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …

no image

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

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

no image

matplotlib で折れ線グラフの下部に色をつけて塗りつぶす方法

pyplot の fill_between を使う。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = & …

no image

python3 で、フォルダがなければ作成するプログラム

os.mkdir() 関数を使う。 すでにフォルダが存在している場合は、エラーを表示する。 例 import os folder_name = ‘./python_create_folder’ try …