2017-08-19 68 views
1
#CODE BLOCK 1 
from pcinput import getInteger 

num1 = getInteger("Number 1: ") 
num2 = getInteger("Number 2: ") 
num3 = getInteger("Number 3: ") 
num4 = getInteger("Number 4: ") 
num5 = getInteger("Number 5: ") 

print("Total is",num1 + num2 + num3 + num4 + num5) 

#CODE BLOCK 2 
from pcinput import getInteger 

total = 0 
count = 0 
while count < 5: 
    total += getInteger("Please give a number: ") 
    count += 1 

print("Total is", total) 

代碼塊1詢問用戶五個數字,並打印總數。但是,該代碼塊使用「輸入數字x:」作爲提示,其中'x'是數字。在While循環中更改提示

如何更改代碼塊2,以便它也使用這種更改提示來詢問每個數字?

+0

增加變量count的值,而不是使用while循環使用條件和記憶來增加您的循環變量之前,你可以使用:'對計在範圍內(1,6):' –

回答

0
total += getInteger("Number {}: ".format(count)) 
0
total+=getInteger("Enter Number {} ".format(count+1)) 

自變量一共是1