前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python入门与基础刷题篇(9)

python入门与基础刷题篇(9)

作者头像
吉师散养基地
发布2022-11-21 14:28:12
2220
发布2022-11-21 14:28:12
举报
文章被收录于专栏:我奏是太阳

目录

题目一:取号(简单)

描述

输入描述:

输出描述:

示例1

作答

题目二:被8整除的数字(简单)

描述

输入描述:

输出描述:

示例1

作答

题目三:门票(简单)

描述

输入描述:

输出描述:

示例1

作答


题目一:取号(简单)

描述

编写一个 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]。

输出描述:

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

示例1

输入:

代码语言:javascript
复制
2
18
quit

输出:

代码语言:javascript
复制
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.

作答

代码语言:javascript
复制
while True:
    print("Welcome! How many people, please?\nEnter 'quit' to end the program.")
    number = input()
    if number == 'quit':
        break
    else:
        number = int(number)
        if number <= 4:
            print('Your small table is reserved!')
        elif number <=6:
            print('Your middle table is reserved!')
        elif number <=10:
            print('Your large table is reserved!')
        elif number >=10:
            print('Sorry, there is no free table that seats over ten persons.')
        continue

题目二:被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]。

输出描述:

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

示例1

输入:

代码语言:javascript
复制
1
16
quit

输出:

代码语言:javascript
复制
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.

作答

代码语言:javascript
复制
active = True
while active == True:
    number = input()
    print("Please enter a positive integer!\nEnter 'quit' to end the program.")
    if number == 'quit':
        active = False
    else:
        number = int(number)
        if number % 8 == 0:
            print(str(number) + ' is a multiple of 8.')
        else:
            print(str(number) + ' is not a multiple of 8.')
        continue

题目三:门票(简单)

描述

某游乐园院按照游客身高段收取票价:不到 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]。

输出描述:

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

示例1

输入:

代码语言:javascript
复制
0.5
1.2
quit

输出:

代码语言:javascript
复制
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.

作答

代码语言:javascript
复制
while True:
    print("Please tell me your height!\nEnter 'quit' to end the program.")
    number = input()
    if number == 'quit':
        break
    else:
        number = float(number)
        if number < 1.0:
            print('Your admission cost is 0 yuan.')
        elif number <= 1.2:
            print('Your admission cost is 80 yuan.')
        elif number > 1.2:
            print('Your admission cost is 150 yuan.')
        continue
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目一:取号(简单)
  • 描述
    • 输入描述:
      • 输出描述:
      • 示例1
      • 作答
      • 题目二:被8整除的数字(简单)
      • 描述
        • 输入描述:
          • 输出描述:
          • 示例1
            • 作答
            • 题目三:门票(简单)
            • 描述
              • 输入描述:
                • 输出描述:
                • 示例1
                  • 作答
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档