zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python入门与基础刷题篇(5)

2023-06-13 09:13:12 时间

目录

题目一:判断列表是否为空(入门)

描述

输入描述:

输出描述:

作答

题目二:禁止重复注册(中等)

描述

输入描述:

输出描述:

作答

题目三:食堂点餐(中等)

描述

输入描述:

输出描述:

作答


题目一:判断列表是否为空(入门)

描述

创建一个空列表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)