2014-09-27 107 views
-1

我正在做一項任務,我不太確定如何從用戶那裏添加輸入值,假設他們都是整數。我試圖用一個while循環來做這件事,但它沒有解決。有沒有更好的方法可以解決這個問題?由於我如何獲得用戶輸入(int)總和

number = 10 
guessed = False 
Sum = 0 
while guessed == False: 
    userInput= int(input("Please enter an integer: ")) 
    value = userInput + Sum 
print(value) 
+0

while循環是解決此問題的有效策略的一部分。發佈你的代碼,你會得到幫助。 – mhawke 2014-09-27 04:22:49

+0

@RafaelBarros數= 10 猜到=假 薩姆= 0 而猜測==題: userInput = INT(輸入( 「請輸入一個整數:」)) 值= userInput +薩姆 打印(值) – 2014-09-27 04:25:07

+0

@ mhawke你能看到它,我做了一個編輯 – 2014-09-27 04:30:54

回答

0

你想要的東西,看起來像這樣:

number = 10 
total_sum = int() 
guess = int(input("Please enter an integer: ")) 
while guess != number: 
    total_sum += guess 
    if total_sum == number: 
     print("Your have guessed the number {}!".format(number)) 
     break 
    elif total_sum > number: 
     print("Your sum of guesses, {}, exceeds the "\ 
       "number {}!".format(total_sum, number)) 
     break 
    guess = int(input("Please enter an integer: ")) 
+0

我正在嘗試製作一個程序,用戶需要猜測一個數字,然後他們的輸入將被累加,直到達到該值或更高。如果它完全匹配,它會輸出你是對的,如果它太高,那麼它會要求用戶再次播放 – 2014-09-27 05:15:01

+0

明白了。固定在上面。 @UmerMuhammad – BUSY 2014-09-27 05:27:25

-1
如果你想添加號碼檢查,如果他們都是整數,如果不將它們轉換成int

。 否則你的所有輸入將被並置。

相關問題