为什么当我设置 var 时没有在 0 计数处循环中断 != 0



我正在为我的班级编写一个程序,该程序是输入您要在线购买的商品,然后是价格。我创建了一个 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()

只有在循环条件中的所有代码都运行完毕后,才会检查循环条件。由于它在那里减少了五倍,所以很有可能它从 2 到 -3,而这些都不等于 0,所以它继续。

此外,您那里或多或少有五倍的相同代码。为什么?只要确保它在那里一次。

并且守卫应该是while totalItems > 0:的,只是一些防御性编程,以确保循环结束,即使错误导致变量低于 0。

最后,不要有变量"cost1"、"cost2"、"cost3"等,特别是如果你事先不知道你需要多少。这就是列表的用途。

你在循环中多次递减变量。

循环将遍历其整个主体,并在执行整个主体后第二次检查条件:

while thing:
do_stuff()
thing = 0
hello()  # this is still executed, even though `thing == 0`
what()  # and this too

看起来你想在循环中的任何位置变为零时自动退出循环totalItems但循环不会这样做,你必须手动完成:

while totalItems != 0:
item1 = input("What's the first item?")  # mind the quotes here
cost1 = input('What is the price of the first item? ')
totalItems = totalItems - 1
if totalItems == 0:
break  # get out of the loop
print(totalItems)
item2 = input("What's the second item?")  # mind the quotes here
cost2 = input("What is the price of the second item?")  # mind the quotes here
totalItems = totalItems - 1
if totalItems == 0:
break  # get out of the loop
print(totalItems)
# and so on

Python 的 Truth 表:"任何非零数都是 Truth"。这意味着while totalItems:是好的。

for 循环可能更适合您的确切需求。

for item_counter in range(total_items): # do stuff

一旦您从 0 变为 total_items,这将停止。这还有一个额外的好处,即不需要自己跟踪所有内容。

您可以设置要打印给用户的字符串的格式。

item = input('Item {}: '.format(item_counter))  # Item 1:
cost = input('How much did {} cost? '.format(item))  # How much did The Internet cost?

如果您希望为"第一个"、"第二个"、"第三个"等项目提供一些聪明的东西,则需要映射它们,但由于这需要无限量的键入,因此给出数字表示通常更容易。101,102,103,104,105,106,107,108,109,110,111,112,113,220,330,440,550,660,770,880,990而你只做到了一百一十一(ish(项目。


扩展词典dict您还可以将项目指定在字典中。

products = dict{}
for loop:
item = ...
if item not in products:
cost = ...
product[item] = cost

这将做的是节省具有无转基因认证的有机无添加糖无反式脂肪减肥水价格的商品,因此如果用户需要该项目的多个副本,他们只需添加一次成本。在此基础上展开,您可以计算添加项目的次数。

...
product[item] = [cost, 1]
else:
product[item][1] += 1

产品[项目]是指作为列表[成本,计数]的值。我们需要修改第二个索引,因此指定 [1],因为我们从 0 开始计数。最终结果看起来有点令人困惑,但最终我们也跟踪了添加的重复项目数量。

在这种情况下,我会使用for-loop

  • while-loop更适合连续运行,直到出现给定条件。
  • for-loop更适合循环遍历rangelist值。
  • 无需对每个项目进行硬编码。 如果有 100 个项目怎么办?
  • 代码中的while-loop不起作用,因为python是顺序的,代码的顺序从item1item5。 直到cost5之后才会再次评估while != 0:
    • 此外,如果totalItems的初始值<= 3,则在重新评估while条件之前,将< 0totalItems。 在这种情况下,循环将永远持续下去。
  • 如果可能,始终重用代码
  • productcost现在位于函数返回的dict
def main():
totalItems = int(input('How many items are you buying? '))
item_dict = dict()
for item in range(1, totalItems + 1):
product = input(f'What is item {item}? ')
cost = input(f'What is the price of item {item}? ')
item_dict[f'item_{item}'] = {'product': product, 'cost': cost}
print('done')
return item_dict
items = main()

输出:

How many items are you buying?  4
What is item 1?  Book
What is the price of item 1?  7.50
What is item 2?  Car
What is the price of item 2?  15000
What is item 3?  Milk
What is the price of item 3?  6.99
What is item 4?  Coffee
What is the price of item 4?  4.99
done

储存items

print(items)

输出:

{'item_1': {'product': 'Book', 'cost': '7.50'},
'item_2': {'product': 'Car', 'cost': '15000'},
'item_3': {'product': 'Milk', 'cost': '6.99'},
'item_4': {'product': 'Coffee', 'cost': '4.99'}}

最新更新