【发布时间】:2020-01-15 06:17:56
【问题描述】:
我需要编写一个短代码,用户可以输入购物行程中的价格,当用户输入 0 时,while 循环将停止。在它停止之后,我需要一个 else 语句来显示项目总数、平均价格和总价。
我是编程新手,我从 python 开始。请让我知道我做错了什么。
price = float
items = float
while price < 0:
items = price
price = input("Please enter prices of items:")
else:
avg = float(sum(items) / len(items))
print('Number of items purchased:', len(items))
print('Average price of items: $', avg)
print('Total price of purchased items: $', sum(items))
当价格
TypeError: 'type' 和 'int' 的实例之间不支持'
【问题讨论】:
-
不会用勺子喂食.. 但是当用户输入 0 时,您需要在 while 循环中使用
break来打破循环......然后,您可以打印语句 -
搜索
break的用法并在控制台上尝试一下。我相信你会弄清楚如何在当前问题中使用它 -
非常感谢您的帮助。因此,当输入 0 时,使用 break 可以让它从 while 循环中继续前进。谢谢大家。
-
price = float应该是什么意思?float是一个类型,而不是一个数字。 -
使用
while True:循环,直到在循环中使用break。
标签: python list if-statement input while-loop