[英]Get a file line input from user, read from a file, return only the int value present on the inputted user line number
[英]Is there a way to keep on asking the user to input a number, so that each number inputted will contribute to the value of an existing dictionary?
這是我正在處理的代碼:
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...elif
。 如果它與自身相等,那么將它自己設置是沒有用的。 while True:
根據需要啟動死循環。 def cva(x)
。 您不想多次定義它。 if inc > str(0)
, if int(inc) > 0
,則替換以檢查inc
是否大於0。 嘗試這個:
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'
def cva(x):
y = cf[ques]
y = float(y)
return x + y
while True:
ques = raw_input('Enter Amount Name: ').lower()
inc = raw_input(ques + ' Enter Amount of Increase: ').lower()
if int(inc) > 0:
cf[ques]=cva(float(inc))
for item in cf:
print item, cf[item]
如果你想讓它永遠重復,你可以嘗試一下while循環。 例如:
p = True
while p == True:
您可以將它放在要重復的代碼的開頭,並在后面的每一行之前選項卡(或放置4個空格)。 此外,如果您想要執行給定次數,您可以執行for循環:
for x in range(1,1000):
#Change the 1000 to whatever number you want
您可以將其放在要重復的代碼前面,也可以將代碼選中。 如果這不能解決您的問題,請評論並說明您需要我解決的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.