如果要存储所有输入的值,然后打印它们,可以使用列表。你的代码会这样结束:#number_enterd = str() # This is totally unnecessary. This does nothing
num = int(input ( "Enter a number: " ))
total = 0 #sum of number
numsEntered = [] # An empty list to hold the values we will enter
numsEntered.append(num) # Add the first number entered to the list
while num != 0:
total += num
num = int(input ( "Enter a number: " ))
#number_enterd = num # Unnecesary as well, this overwrites what you wrote in line 2
# It doesn't even make the num a string
numsEntered.append(num) #This adds the new num the user just entered into the list
print("Total is =", total )
print("Numbers entered:", numsEntered) #check to see the numbers you've collected
例如,用户输入5,2,1,4,5,7,8,0作为num input请求的输入。
您的输出将是:
^{pr2}$
作为一个向导,我会这样做的。希望有帮助:num = int(raw_input("Enter a number: "))
numsEntered = [] # An empty list to hold the values we will enter
total = 0 #sum of numbers
while True:
numsEntered.append(num) #This adds the num the user just entered into the list
total += num
num = int(raw_input("Enter a number: "))
print("Total is =", total)
print("Numbers entered:", numsEntered) #check to see the numbers you've collected
在本例中,最后一个输入以退出循环的0不会出现。至少,我不想让它出现,因为它对总数没有任何影响。
希望你喜欢我的回答!祝你今天愉快:)