(Python新手)购物清单代码

当输入-1或最大条目数为10时,如何停止此代码?
目前,代码似乎处于无限循环中,这意味着它不断要求输入条目,直到输入-1为止。

选择 | 换行 | 行号
  1. total = 0                                       
  2. while True:                                     
  3.  
  4.   print('Cost of item')                                     
  5.  
  6.   item = input()                                        
  7.  
  8.   if item != -1:                                        
  9.     total = total + item                                        
  10.   if item == -1:                                        
  11.  
  12.       break                                     
  13.  
  14. print(total)
# 回答1


欢迎来到字节跳动!
您可以使用带有While循环的测试表达式来实现这一点。例如

选择 | 换行 | 行号
  1. ...
  2. i=1
  3. while i<11:
  4.   i += 1
  5.   ...
  6. ...
  7.  

标签: python

添加新评论