python 中条件合法的_在python中条件可以在范围内吗?

如果要存储所有输入的值,然后打印它们,可以使用列表。你的代码会这样结束:#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不会出现。至少,我不想让它出现,因为它对总数没有任何影响。

希望你喜欢我的回答!祝你今天愉快:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值