前言
最近很多学了基础的小伙伴问我该怎么提升编程水平?学了基础该上哪刷题?明明学了很多,做项目却不知道怎么上手,其实这就是练得太少,只注重了学,却忽视了刷题,只有不断练习才能提高和巩固编程思维和能力!
刚好看到牛客网最近出了Python的新题库于是体验了一番感觉还不错
链接地址:牛客网 | Python从入门到实践四十招,废话少说速度上号,或者跟着下文一起刷题!!!
描述: 假如这是一台自动售卖饮料机的一段程序:
使用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!
描述:
编写一个 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.
描述:
编写一个 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.
描述:
某游乐园院按照游客身高段收取票价:不到 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.
嫌博主更新慢的小伙伴牛客网上号自行刷题
链接地址:牛客网 | Python从入门到实践四十招,废话少说速度上号!!!