首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我在我的代码python中做错了什么,它不会打印总成本?

我在我的代码python中做错了什么,它不会打印总成本?
EN

Stack Overflow用户
提问于 2015-06-22 11:22:02
回答 4查看 95关注 0票数 1

此代码需要允许输入最多3个想要的物品,并打印所有物品的总成本。我对这一切都是新手,需要我能得到的所有建议。我不能把总数打印出来。

代码语言:javascript
运行
复制
pie = 2.75
coffee = 1.50
icecream = 2.00

while choice:

    choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")

    if choice01 == "nothing":
        break

    choice02 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")

    if choice02 == "nothing":
        break

    choice03 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")

    if choice03 == "nothing":
        break

    cost = choice01+choice02+choice03


    print "Your total is: ${0}" .format(cost)
EN

Stack Overflow用户

发布于 2015-06-22 11:33:40

看起来你根本不需要三个选择。考虑以下代码

代码语言:javascript
运行
复制
pie = 2.75
coffee = 1.50
icecream = 2.00
cost = 0
while True:
    choice01 = 0
    choice01 = raw_input("What would you like to buy? pie, coffee, icecream, or nothing?")

    if choice01 == "nothing":
        break
    elif choice01=="coffee":
        choice01 = coffee
    elif choice01=="pie":
        choice01 = pie
    elif choice01=="icecream":
        choice01==icecream
    else:
        choice01=0
    cost+=choice01

print "Your total is: ${0}" .format(cost)
票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30971683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档