练习题,三个出口,python编程从入门到实践p110

三个出口:以另一种方式完成练习7-4 或练习7-5,在程序中采取如下所有做法

  • 在while 循环中使用条件测试来结束循环。
  • 使用变量active 来控制循环结束的时机。
  • 使用break 语句在用户输入’quit’时退出循环。
    Jackey已对题目稍作修改。代码如下:
// An highlighted block
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

# 提示
prompt = "How old are you? "
prompt += "\n(Enter 'quit' when you are finished.)  "

# 运行状态
active = True  # 标志

while active:
    age = input(prompt)
    if age == 'quit':
        print("\nYou have logged out of the system.Thank you.")
        break  # 退出运行
    else:
        age = int(age)
        if age < 0:
            print("\n\t▲▲▲ Good luck to you during pregnancy! ▲▲▲\n") # 母胎孕育中,祝你好运!
        elif age <= 3:
            print(f"\n\t▲▲▲ You have {age} old,the ticket for free. ▲▲▲\n")
        elif age <= 12:
            print(f"\n\t▲▲▲ You have {age} old,the ticket is 10$. ▲▲▲\n")
        elif age <= 60:
            print(f"\n\t▲▲▲ You have {age} old,the ticket is 15$. ▲▲▲\n")
        elif age < 100:
            print(f"\n\t▲▲▲ You have {age} old,the ticket for free. ▲▲▲\n")
        else:
            print("You're so funny. Bye!")
            active = False # 结束循环
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值