Python入门与基础刷题篇(5)
目录
题目一:判断列表是否为空(入门)
描述
创建一个空列表my_list,如果列表为空,请使用print()语句一行输出字符串'my_list is empty!',
否则使用print()语句一行输出字符串'my_list is not empty!'。
输入描述:
无
输出描述:
按题目描述进行输出即可。
作答
my_list = []
if my_list == []:
print("my_list is empty!")
else:
print("my_list is not empty!")
题目二:禁止重复注册(中等)
描述
创建一个依次包含字符串'Niuniu'、'Niumei'、'GURR'和'LOLO'的列表current_users,
再创建一个依次包含字符串'GurR'、'Niu Ke Le'、'LoLo'和'Tuo Rui Chi'的列表new_users,
使用for循环遍历new_users,如果遍历到的新用户名在current_users中,
则使用print()语句一行输出类似字符串'The user name GurR has already been registered! Please change it and try again!'的语句,
否则使用print()语句一行输出类似字符串'Congratulations, the user name Niu Ke Le is available!'的语句。(注:用户名的比较不区分大小写)
输入描述:
无
输出描述:
按题目描述进行输出即可。
The user name GurR has already been registered! Please change it and try again! Congratulations, the user name Niu Ke Le is available! The user name LoLo has already been registered! Please change it and try again! Congratulations, the user name Tuo Rui Chi is available!
作答
current_users = ['Niuniu', 'Niumei', 'GURR', 'LOLO']
new_users = ['GurR', 'Niu Ke Le', 'LoLo', 'Tuo Rui Chi']
current_users_l = [user.lower() for user in current_users]
for new_user in new_users:
if new_user.lower() in current_users_l:
print(f"The user name {new_user} has already been registered! \
Please change it and try again!")
else:
print(f"Congratulations, the user name {new_user} is available!")
题目三:食堂点餐(中等)
描述
某食堂今天中午售卖 'pizza':10块钱一份,'rice' :2块钱一份,'yogurt':5块钱一份,剩下的其他菜品都是8块钱一份。
请创建如下一个order_list记录点单情况:
['rice', 'beef', 'chips', 'pizza', 'pizza', 'yogurt', 'tomato', 'rice', 'beef']
然后使用for循环遍历列表order_list,使用if-elif-else结构依次打印每份菜品及其价格,且每个菜品都独占一行,按照'beef is 8 dollars'的形式。
并且在遍历过程中将价格相加,求对于这些点单记录,食堂总共营业收入多少?(单独输出一个整数)
输入描述:
无
输出描述:
按题目描述进行输出即可。
rice is 2 dollars
beef is 8 dollars
chips is 8 dollars
pizza is 10 dollars
pizza is 10 dollars
yogurt is 5 dollars
tomato is 8 dollars
rice is 2 dollars
beef is 8 dollars
61
作答
order_list = ['rice', 'beef', 'chips', 'pizza', 'pizza', 'yogurt', 'tomato', 'rice', 'beef']
sum = 0
def food_price(food: str, price: int):
global sum
sum += price
print("{0} is {1} dollars".format(food, str(price)))
for food in order_list:
if food == "pizza":
food_price(food, 10)
elif food == "rice":
food_price(food, 2)
elif food == "yogurt":
food_price(food, 5)
else:
food_price(food, 8)
print(sum)
相关文章
- aic准则python_Python数据科学:线性回归
- Python入门系列(三)基础数据类型
- python与anaconda安装(先安装了python后安装anaconda,基于python已存在的基础上安装anaconda)——逼死强迫症、超详解[通俗易懂]
- python基础系列教程——Python的安装与测试:python解释器、PyDev编辑器、pycharm编译器
- python基础(3)列表list[通俗易懂]
- Python 入门与基础《刷题篇》(1)
- Python 入门与基础《刷题篇》(3)
- Python入门与基础刷题篇(4)
- Python入门与基础刷题篇(6)
- python入门与基础刷题篇(9)
- python入门基础(11)
- Python学习之面向对象基础
- Python文件名后缀_python获取目录下所有文件的文件名
- python制作自动交易程序_Python如何实现自动化交易
- python自动化测试—Python自动化框架及工具
- 新手学Python可行吗?需要什么基础?(新手学python可行吗?需要什么基础)
- 《Python程序设计基础与应用(第2版)》课后习题参考答案
- 基础入门详解Python 操作 Excel 自动化办公指南
- Python基础语法-控制流程语句-while
- Python基础语法-函数的高级用法-闭包
- Linux创建Python文件的步骤(linux新建python文件)
- 下的应用玩转Python:Linux下的Python应用之旅(python在linux)
- Linux中如何离开Python环境(linux怎么退出python)