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

简介: 速刷n题

题目一:取号(简单)

描述

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

输入:


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.

作答

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

输入:


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.

作答

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

输入:


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.

作答

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


目录
相关文章
|
6天前
|
Python
Python 编程入门:打造你的第一个程序
【9月更文挑战第27天】编程,就像是在数字世界里绘画。想象一下,你手中的键盘是画笔,屏幕是画布,而代码则是你的颜料。这篇文章将带你走进编程的世界,学习如何使用 Python 这门语言来创建你的第一个程序。我们将从基础的语法开始,逐步深入到条件判断和循环结构,最终完成一个简单的猜数字游戏。无论你是否有编程经验,这里的内容都将为你打开一扇新的大门。
|
6天前
|
人工智能 数据挖掘 开发者
Python编程入门:从零到英雄
【9月更文挑战第27天】本文旨在通过浅显易懂的语言,为初学者介绍Python编程的基础知识和实用技巧。我们将一起探索Python的世界,了解其语法、数据结构,并通过实际示例学习如何编写简单的Python程序。无论你是编程新手,还是希望拓展技能的开发者,这篇文章都将为你打开一扇通往Python编程世界的大门。
|
4天前
|
Python
? Python 装饰器入门:让代码更灵活和可维护
? Python 装饰器入门:让代码更灵活和可维护
11 4
|
7天前
|
存储 人工智能 数据挖掘
Python编程入门:从基础到实战
【9月更文挑战第26天】 在这篇文章中,我们将一起探索Python编程的奇妙世界。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供有价值的信息和技巧。我们将从Python的基本语法开始,然后逐步深入到更复杂的主题,如函数、类和模块。最后,我们将通过一个实际的项目来应用我们所学的知识。让我们一起开始这段Python编程之旅吧!
|
4天前
|
数据可视化 Python
使用Python进行数据可视化:从入门到精通
【8月更文挑战第60天】本文是一篇面向初学者的Python数据可视化教程,旨在帮助读者掌握如何使用Python及其强大的库(如Matplotlib和Seaborn)来创建引人入胜的数据可视化。我们将从基础开始,逐步深入,最终达到能够独立完成复杂数据可视化项目的水平。无论你的背景如何,只要你对数据可视化感兴趣,这篇文章都将为你开启一段新的学习之旅。
|
7天前
|
数据采集 人工智能 数据挖掘
Python编程入门:从基础到实战的快速指南
【9月更文挑战第25天】本文旨在为初学者提供一个简明扼要的Python编程入门指南。通过介绍Python的基本概念、语法规则以及实际案例分析,帮助读者迅速掌握Python编程的核心技能。文章将避免使用复杂的专业术语,而是采用通俗易懂的语言和直观的例子来阐述概念,确保内容的可读性和实用性。
|
5天前
|
Python
Python 装饰器入门:让代码更灵活和可维护
Python 装饰器入门:让代码更灵活和可维护
10 1
|
7天前
|
设计模式 开发者 Python
Python中的装饰器:从入门到精通
【9月更文挑战第25天】本文深入浅出地介绍了Python装饰器的使用,包括其定义、语法和实际应用。通过实例演示如何利用装饰器增强函数功能,同时探讨了装饰器的高级用法如带参数的装饰器和装饰器嵌套。最后,文章强调了在设计装饰器时应避免的常见陷阱。
|
7天前
|
前端开发 开发者 Python
从零到一:Python Web框架中的模板引擎入门与进阶
在Web开发的广阔世界里,模板引擎是连接后端逻辑与前端展示的重要桥梁。对于Python Web开发者而言,掌握模板引擎的使用是从零到一构建动态网站或应用不可或缺的一步。本文将带你从基础入门到进阶应用,深入了解Python Web框架中的模板引擎。
14 3
|
7天前
|
Python Windows
python入门保姆级教程 | 13
python入门保姆级教程 | 13
下一篇
无影云桌面