2017-11-19 10 views
-1

プログラムに辞書(下のコード)に格納されている入力をユーザーに入力するよう要求しています。私は最初に存在するエントリの数をユーザに尋ね、エントリが入るときにそれらを数えなければなりません。カウントを使用しましたが、ユーザー定義のカウント制限を実装する方法はわかりません。ユーザーが入力数を決定した後にユーザー入力から辞書を作成する(Python)

entriesDict = {} 

while True: 
    x = input('Please enter the name: ') 
    y = input('Please enter their score: ') 
    if x == '': 
     break 
    entriesDict[x] = y 
print(entriesDict) 

答えて

0

エントリの数を判断するには、まずユーザー入力を1回取得する必要があります。次に、forループを使用してエントリを繰り返し取得します。

entriesDict = {} 

num_entries = input('Please enter the number of entries: ') 

for i in range(num_entries): 
    x = input('Please enter the name: ') 
    y = input('Please enter their score: ') 
    entriesDict[i] = y 
print(entriesDict) 
+1

私は理解しています!ありがとうございます、私はfor-loopを使用するとは考えず、エントリ数で範囲を設定しました。もし私が代理人を持っていたら、私はアップヴォートするだろう。 – saj0991

関連する問題