1. 找到HR
描述:创建一个依次包含字符串’Niuniu’、‘Niumei’、‘HR’、‘Niu Ke Le’、‘GURR’ 和 ‘LOLO’ 的列表users_list,
使用for循环遍历users_list,如果遍历到的用户名是 ‘HR’ ,则使用print()语句一行打印字符串 ‘Hi, HR! Would you like to hire someone?’,否则使用print()语句一行打印类似字符串 ‘Hi, Niuniu! Welcome to Nowcoder!’ 的语句。
输入描述:无
输出描述:按题目描述进行输出即可。
Hi, Niuniu! Welcome to Nowcoder!
Hi, Niumei! Welcome to Nowcoder!
Hi, HR! Would you like to hire someone?
Hi, Niu Ke Le! Welcome to Nowcoder!
Hi, GURR! Welcome to Nowcoder!
Hi, LOLO! Welcome to Nowcoder!
实现代码:
users_list=['Niuniu','Niumei','HR','Niu Ke Le','GURR','LOLO']
for i in users_list:
if i=="HR":
print("Hi, HR! Would you like to hire someone?")
else:
print(f"Hi, {i}! Welcome to Nowcoder!")
运行结果:
Hi, Niuniu! Welcome to Nowcoder!
Hi, Niumei! Welcome to Nowcoder!
Hi, HR! Would you like to hire someone?
Hi, Niu Ke Le! Welcome to Nowcoder!
Hi, GURR! Welcome to Nowcoder!
Hi, LOLO! Welcome to Nowcoder!
2. 判断列表是否为空
描述:创建一个空列表my_list,如果列表为空,请使用print()语句一行输出字符串’my_list is empty!‘,
否则使用print()语句一行输出字符串’my_list is not empty!’。
输入描述:无
输出描述:按题目描述进行输出即可
实现代码:
my_list=[]
if my_list:
print("my_list is not empty!")
else:
print("my_list is empty!")
运行结果:
my_list is empty!
3. 禁止重复注册
描述: 创建一个依次包含字符串’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_up = [i.upper() for i in current_users]
for i in new_users:
if i.upper() in current_users_up:
print('The user name {} has already been registered! Please change it and try again!'.format(i))
else:
print('Congratulations, the user name {} is available!'.format(i))
运行结果:
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!
4. 食堂点餐
描述: 某食堂今天中午售卖 ‘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']
#定义一个价格表
price_list = [2,8,8,10,10,5,8,2,8]
new_order_list = [[order_list[i],price_list[i]] for i in range(len(order_list))]
#print(new_order_list)
for i in range(len(new_order_list)):
for j in range(1):
#print(new_order_list[i][j])
#print(new_order_list[i][j+1])
print(new_order_list[i][j]+ ' is ' +str(new_order_list[i][j+1])+' dollars')
break
print(sum(price_list))
运行结果:
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