2014-10-22 26 views
0
滿足用戶輸入的總和
total = 0 
not_total = True 
sum_of_square = int(input("Enter desired sum: ")) 

while (not_total == True): 
     for n in range (2): 
     total = total + n**2 
     print("The sum of squares from 1^2 to {0}^2 is equal to {1}.".format(n,total)) 

後,這將提供資金無限量,但我希望用戶輸入之前停止總和完一個程序在Python

回答

0

你可以只在有條件的兩個值進行比較while循環的說法,像這樣:

total = 0 
sum_of_square = int(input("Enter desired sum: ")) 

while (sum_of_squares <= total): 
     for n in range (2): 
     total = total + n**2 
     print("The sum of squares from 1^2 to {0}^2 is equal to {1}.".format(n,total)) 
+0

我試過那件編碼,但它回來了作爲一個無限循環,但如果聲明中這樣我試圖把一個... 總= 0 not_total =真(sum_of_square = int)(input(「Enter desired sum:」)) while(not_total == True): for n in range(1,100,1): total = total + n ** 2 if total 2014-10-22 11:38:14

+0

您可能會超過總數。我更新了代碼,試試。 – Parker 2014-10-22 15:43:20

相關問題