Python每日一练——第23天:综合练习

1. 自动售卖饮料机

描述: 假如这是一台自动售卖饮料机的一段程序:
使用print()语句一行输出字符串 ‘What kind of drink would you like?’ ,
然后使用input()函数读取字符串,并将读取到的字符串存储到变量kind_of_drink中,
假设读取到饮料是可乐(cola),也即变量kind_of_drink的内容为’cola’,
请使用print()语句一行输出字符串 ‘Below is your cola. Please check it!’ 。
其他饮料已经售空了,因此如果是其他字符串,则输出一句类似 ‘The milk has been sold out!’ 的信息。

输入描述:无

输出描述:按题目描述进行输出即可。

实现代码:

print("What kind of drink would you like?")
kind_of_drink = input()
if kind_of_drink in "cola":
    print("Below is your cola. Please check it!")
else:
    print("The", kind_of_drink, "has been sold out!")

运行结果:

cola
What kind of drink would you like?
Below is your cola. Please check it!

2. 取号

描述
编写一个 while 循环模拟餐厅服务员询问客人一共有多少人用餐,要求在 while 循环中使用条件测试来结束循环。
每次循环开始先使用print()语句一行输出字符串 “Welcome! How many people, please?\nEnter ‘quit’ to end the program.”,
如果读取到字符串等于’quit’,则退出 while 循环,
否则将字符串转成整数,如果整数不超过4,则使用print()语句一行输出字符串 ‘Your small table is reserved!’;
如果整数大于4不超过6,则使用print() 语句一行输出字符串 ‘Your middle table is reserved!’;
如果整数大于6不超过10,则使用print() 语句一行输出字符串 ‘Your large table is reserved!’;
如果整数超过10,则使用print()语句一行输出字符串 ‘Sorry, there is no free table that seats over ten persons.’;
然后本次循环结束,再次进入 while 循环中的条件测试。

输入描述:保证每一行的输入只有数字或字符串’quit’,且保证数字合法,范围在[1, 20]。

输出描述:按题目描述进行输出即可。

实现代码:

#  保证每一行的输入只有数字或字符串'quit',且保证数字合法,范围在[1, 20]。
while True:
    print("Welcome! How many people, please?\nEnter 'quit' to end the program.")
    try:
        a=input()
        if a == 'quit':
            break
        else:
            a=int(a)
            if a not in [i for i in range(1,21)]:
                print('数字不合法')
            else:
                if a<=4:
                    print('Your small table is reserved!')
                elif 4 < a <= 6:
                    print('Your middle table is reserved!')
                elif 6 < a <= 10:
                    print('Your large table is reserved!')
                elif a>10:
                    print('Sorry, there is no free table that seats over ten persons.')
    except:
        print('请输入1-20整数(退出请输入:quit)')
        continue

运行结果:

2
18
quit
Welcome! How many people, please?
Enter 'quit' to end the program.
Your small table is reserved!
Welcome! How many people, please?
Enter 'quit' to end the program.
Sorry, there is no free table that seats over ten persons.
Welcome! How many people, please?
Enter 'quit' to end the program.

3. 被8整除的数字

描述
编写一个 while 循环判断输入的字符串对应的十进制数值是否是被8整除的数字,要求使用布尔变量 active 来控制循环结束的时机。
每次循环开始先使用print()语句一行输出字符串 “Please enter a positive integer!\nEnter ‘quit’ to end the program.” ,
如果读取到字符串等于’quit’,则把布尔变量 active 的值更改为False,
否则将字符串转成整数,如果能被8整除即是8的倍数,则使用print()语句一行输出类似字符串’80 is a multiple of 8.'的语句,
否则使用print()语句一行输出类似字符串’4 is not a multiple of 8.‘的语句,
然后本次循环结束,再次进入 while 循环中的条件测试。

输入描述:保证每一行的输入只有数字或字符串’quit’,且保证数字合法,范围在[1, 100]。

输出描述:按题目描述进行输出即可。

实现代码:

active = True
while active:
    print("Please enter a positive integer!\nEnter 'quit' to end the program." )
    msg=input()
    if msg == 'quit':
        active = False
    else:
        msg = int(msg)
        i=msg%8
        if i==0:
            print("{} is a multiple of 8.".format(msg))
        else:
            print('{} is not a multiple of 8.'.format(msg))
        continue

运行结果:

1
16
quit
Please enter a positive integer!
Enter 'quit' to end the program.
1 is not a multiple of 8.
Please enter a positive integer!
Enter 'quit' to end the program.
16 is a multiple of 8.
Please enter a positive integer!
Enter 'quit' to end the program.

4. 游乐园的门票

描述
某游乐园院按照游客身高段收取票价:不到 1.0米 的游客免费; 1.0~1.2 米的游客为 80 元;超过 1.2 米的游客为 150 元。
请编写一个死循环,每次循环开始先使用print()语句一行输出字符串"Please tell me your height!\nEnter ‘quit’ to end the program."。
如果读取到的字符串等于’quit’,则使用 break 语句退出循环;
否则将字符串转成浮点数,如果小于1.0米,则使用print()语句一行输出字符串’Your admission cost is 0 yuan.‘,
如果大于等于1.0米且小于等于1.2米,则使用print()语句一行输出字符串’Your admission cost is 80 yuan.’,
如果大于1.2米,则使用print()语句一行输出字符串’Your admission cost is 150 yuan.‘,
然后本次循环结束,再次进入 while 循环中的条件测试。

输入描述:保证每一行的输入只有浮点数或字符串’quit’,且保证数字合法,范围在[0, 3]。

输出描述:按题目描述进行输出即可。

代码实现:

while True:
    print("Please tell me your height!\nEnter 'quit' to end the program.")
    height = input()
    if height.lower() == 'quit':
        break
    elif float(height) < 1.0:
        print('Your admission cost is 0 yuan.')
    elif float(height) <= 1.2:
        print('Your admission cost is 80 yuan.')
    elif float(height) > 1.2:
        print('Your admission cost is 150 yuan.')
    else:
        print('input error')

运行结果:

0.5
1.2
quit
Please tell me your height!
Enter 'quit' to end the program.
Your admission cost is 0 yuan.
Please tell me your height!
Enter 'quit' to end the program.
Your admission cost is 80 yuan.
Please tell me your height!
Enter 'quit' to end the program.
  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁袁袁袁满

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值