【发布时间】:2019-09-20 04:45:28
【问题描述】:
我正在为我的班级编写一个程序,该程序是输入您要在线购买的商品,然后输入价格。我创建了一个 while 循环,一旦用户购买的物品总数变为零,它就会中断,这样我就可以接收他们想要的所有物品。出于某种原因,尽管当变量 totalItems 达到零时(我知道这是因为我将每一行都打印出来)循环并没有中断,实际上它一直处于负数。
def main():
totalItems = int(input('How many items are you buying? '))
while totalItems != 0:
item1 = input('What is the first item? ')
cost1 = input('What is the price of the first item? ')
totalItems = totalItems - 1
print(totalItems)
item2 = input('What is the second item? ')
cost2 = input('What is the price of the second item? ')
totalItems = totalItems - 1
print(totalItems)
item3 = input('What is the third item? ')
cost3 = input('What is the price of the third item? ')
totalItems = totalItems - 1
print(totalItems)
item4 = input('What is the fourth item? ')
cost4 = input('What is the price of the first item? ')
totalItems = totalItems - 1
print(totalItems)
item5 = input('What is the first item? ')
cost5 = input('What is the price of the first item? ')
totalItems = totalItems - 1
print('done')
main()
【问题讨论】:
-
如果
totalItems低于 0 会怎样?
标签: python while-loop