题目一:取号(简单)
描述
编写一个 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