這裏是代碼我的工作:有沒有辦法不斷要求用戶輸入一個數字,以便輸入的每個數字將有助於現有字典的價值?
ans = raw_input('Enter Amount of Players: ').lower()
cf={}
if ans == '2':
p1 = raw_input('What is Player 1 named:')
p2 = raw_input('What is Player 2 named:')
cf[p1] = '50'
cf[p2] = '50'
ques = raw_input('Enter Amount Name: ').lower()
if ques == p1:
ques = p1
elif ques == p2:
ques = p2
elif ques == p3:
ques = p3
elif ques == p4:
ques = p4
elif ques == p5:
ques = p5
elif ques == p6:
ques = p6
elif ques == p7:
ques = p7
elif ques == p8:
ques = p8
inc = raw_input(ques + ' Enter Amount of Increase: ').lower()
if inc > str(0):
def cva(x):
y = cf[ques]
y = float(y)
return x + y
num = inc
num = float(num)
cf[ques]=cva(num)
for item in cf:
print item, cf[item]
我如何獲得這樣的:
ques = raw_input('Enter Amount Name: ').lower()
if ques == p1:
ques = p1
elif ques == p2:
ques = p2
elif ques == p3:
ques = p3
elif ques == p4:
ques = p4
elif ques == p5:
ques = p5
elif ques == p6:
ques = p6
elif ques == p7:
ques = p7
elif ques == p8:
ques = p8
inc = raw_input(ques + ' Enter Amount of Increase: ').lower()
if inc > str(0):
def cva(x):
y = cf[ques]
y = float(y)
return x + y
num = inc
num = float(num)
cf[ques]=cva(num)
for item in cf:
print item, cf[item]
要不斷重複無限?我的意思是當用戶輸入一個數字時,該數字將被添加到字典中已經存在的值中。有沒有辦法不斷要求用戶輸入一個數字,以便輸入的每個數字都能爲詞典的價值做出貢獻?
你想通過做所有這些if語句來完成什麼?你正在檢查它是否等於一個特定的值,如果它確實,那麼你將它設置爲它已經等於的值? – user2896976
您的代碼難以閱讀;你應該澄清預期的結果。目標(1)輸入一個字符串,(2)輸入一個值,(3)通過值增加與字符串相關的字典值? – ChrisP