繁体   English   中英

从空列表开始并使用for循环中的输入添加到列表时遇到麻烦

[英]Having trouble starting with an empty list and adding to it using an input within a for loop

在学校的编程课上,我被要求做以下事情:

从一个空的coffee_price_list开始。 使用while循环询问拿铁的价格或负数以停止增加价格。 在while循环内,询问咖啡价格或负数以停止。 将正咖啡价格追加到您的coffee_price_list。 完成后,程序将查找列表中有多少项。 从列表中打印每个价格,每行一个价格。

我已经走了这么远,我相信我已经快要解决问题了,但还是会遇到问题。

coffee_price_list = [ ]
coffee = 0
while coffee > 0:
    coffee = int(input("Enter price of coffee or a negative value to 
    stop: "))
    if coffee < 0:
        coffee_price_list.append(coffee)
for price in coffee_price_list:
    print(price)

任何帮助将不胜感激!

如果咖啡价格不能为零,请使用此选项

coffee = 1 # or any other positive number
while coffee > 0:
    coffee = int(input("Enter price of coffee or a negative value to 
stop: "))
    if coffee > 0:

如果咖啡价格可以为零,请使用此

coffee = 0
while coffee >= 0:
    coffee = int(input("Enter price of coffee or a negative value to 
stop: "))
    if coffee >= 0:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM