首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在开发一个购买电影票的程序。试着弄清楚我是否需要一个while循环来完成我想要完成的任务?

正在开发一个购买电影票的程序。试着弄清楚我是否需要一个while循环来完成我想要完成的任务?
EN

Stack Overflow用户
提问于 2020-11-09 06:52:54
回答 2查看 648关注 0票数 0

我正在做一个电影票的节目。程序必须计算一张电影票的价格。该程序应该提示用户顾客的年龄和电影是否为3D。儿童和老年人应享受折扣价格。3D电影应该有附加费。程序应根据输入的年龄和电影是否为3D格式输出电影票的票价。我已经完成了一半的程序。儿童和老年人可享受折扣价格。

我几乎完成了程序,但当我询问电影是否为3D时,它会打印出相同的是和否的回答。当用户输入"No“时,我想让它说门票的价格保持不变。而且,当他们输入"Yes“时,它需要显示3D门票的新价格。当输入" yes“时,它已经做了正确的事情,但问题是,当输入" no”时,它显示相同的输出,不确定我是否需要将yes和no存储在另一个变量中,或者可能是while循环中?有谁能告诉我怎么走吗?任何帮助都是非常感谢的。

代码语言:javascript
复制
age = int(input("Welcome to the movie theatre. What is your age? Children and senior citizens will receive a discount.   "))

children_ticket = 8

adult_ticket = 10

senior_ticket = 8

if age <= 12:
    print("The children's ticket costs"  ,children_ticket)
    
if age >= 65:
    print("The senior citizens ticket costs" ,senior_ticket)

if (age >= 13) and (age <= 64):
    print("The adult ticket costs" ,adult_ticket)


three_d = input("Is the movie you're watching 3D? If so, they have a surcharge.  ")

three_d_surcharge = 2

if age <= 12:
    print("The children's ticket for 3D costs"  ,children_ticket + three_d_surcharge)
    
if age >= 65:
    print("The senior citizens ticket for 3D costs" ,senior_ticket + three_d_surcharge)

if (age >= 13) and (age <= 64):
    print("The adult ticket for 3D costs" ,adult_ticket + three_d_surcharge)

3D movie ticket results

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-09 07:13:41

您需要将three_d的结果包装在if语句中,如果用户输入Yes,则.lower()允许更宽的输入范围

代码语言:javascript
复制
age = int(input("Welcome to the movie theatre. What is your age? Children and senior citizens will receive a discount.   "))

children_ticket = 8

adult_ticket = 10

senior_ticket = 8

if age <= 12:
    print("The children's ticket costs", children_ticket)

if age >= 65:
    print("The senior citizens ticket costs", senior_ticket)

if (age >= 13) and (age <= 64):
    print("The adult ticket costs", adult_ticket)

three_d = input("Is the movie you're watching 3D? If so, they have a surcharge.  ")

three_d_surcharge = 2

if three_d.lower() == "yes":
    if age >= 65:
        print("The senior citizens ticket for 3D costs", senior_ticket + three_d_surcharge)
    elif age > 12:
        print("The adult ticket for 3D costs", adult_ticket + three_d_surcharge)
    else:
        print("The children's ticket for 3D costs", children_ticket + three_d_surcharge)
else:
    print("No 3d surcharge")

您还可以更改if检查的工作方式,以减少检查次数。

票数 0
EN

Stack Overflow用户

发布于 2020-11-09 07:00:36

您必须为three_d变量执行if else语句。就像这样

代码语言:javascript
复制
if three_d=='yes':
    ...
else:
    print("the price of the ticket stays the same")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64743859

复制
相关文章

相似问题

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