目录
打算开个新坑,将刷题篇的10篇文章的中等题做详细解析。
创建一个依次包含字符串'chichen'、' bacon'和'durian'的列表 pizza_orders,再创建一个名为 finished_pizza 的空列表,
使用 while 循环判断列表 pizza_orders 里面是否还有元素,如果有则使用pop()方法删掉将列表 pizza_orders 的最后一个元素,并把刚刚删掉的元素存到一个名为pizza的变量,假设pizza的值为'bacon',请使用print()语句一行打印类似字符串'I made your bacon pizza!'的语句,并使用append()语句将pizza添加到列表 finished_pizza 的末尾,然后本次循环结束,再次进入 while 循环中的条件测试。
在 while 循环结束后,再使用print()语句把列表 finished_pizza 整个打印出来。
无
按题目描述进行输出即可。
I made your durian pizza! I made your bacon pizza! I made your chichen pizza! ['durian', 'bacon', 'chichen']
pizza_orders = ['chichen','bacon','durian']
finished_pizza = []
while pizza_orders != []:
pizza = pizza_orders.pop()
if pizza == 'durian':
print('I made your durian pizza!')
elif pizza == 'bacon':
print("I made your bacon pizza!")
elif pizza == 'chichen':
print('I made your chichen pizza!')
finished_pizza.append(pizza)
continue
print(finished_pizza)
创建一个依次包含字符串'bacon'、'durian'、'bacon'、'bacon'、'chicken'和'durian'的列表pizza_inventory,
使用 while循环 判断字符串'bacon'是否存在于列表pizza_inventory中,
如果存在,则使用remove()方法删掉列表pizza_inventory中的一个字符串'bacon',并使用print()语句一行输出字符串'A bacon pizza was deleted from list.',
然后本次循环结束,再次进入 while 循环中的条件测试。
在 while 循环结束后,如果if语句判断字符串'bacon'确实不在列表pizza_inventory中,请使用print()语句一行输出字符串'There is really no bacon in pizza_inventory!'。
无
按题目描述进行输出即可。
pizza_inventory=['bacon','durian','bacon','bacon','chicken','durian']
while('bacon' in pizza_inventory):
pizza_inventory.remove('bacon')
print('A pastrami order was deleted from list.')
if('bacon' not in pizza_inventory):
print('There is really no pastrami in sandwich_orders!')
创建一个名为survey_dict的空字典,
请编写一个死循环,
每次循环开始先使用print()语句一行输出字符串'If you have the chance, which university do you want to go to most?',
再使用print()语句一行输出字符串'What is your name?',再将读取到的字符串存储在变量name中, 再使用print()语句一行输出字符串'Which university do you want to go to most?',再将读取到的字符串存储在变量university中,
再把键-值对name: university存储在字典survey_dict中, 再使用print()语句一行输出字符串"Is there anyone who hasn't been investigated yet?",
如果输入的字符串为'No',则使用 break 语句退出循环,否则本次循环结束,再次进入 while 循环中的条件测试。
在 while 循环结束后,使用for循环遍历 已使用sorted()函数按升序进行临时排序的包含字典survey_dict的所有键的列表,
对于每一个遍历到的被调查者的名字,使用print()语句一行输出类似字符串"I'am Tom. I'd like to go to Fudan University if I have the chance!"的语句。
无
按题目描述进行输出即可。
输入:
Tom
Fudan University
Yes
Ben
Peking University
Yes
Andy
Tsinghua University
No
复制输出:
If you have the chance, which university do you want to go to most?
What is your name?
Which university do you want to go to most?
Is there anyone who hasn't been investigated yet?
If you have the chance, which university do you want to go to most?
What is your name?
Which university do you want to go to most?
Is there anyone who hasn't been investigated yet?
If you have the chance, which university do you want to go to most?
What is your name?
Which university do you want to go to most?
Is there anyone who hasn't been investigated yet?
I'am Andy. I'd like to go to Tsinghua University if I have the chance!
I'am Ben. I'd like to go to Peking University if I have the chance!
I'am Tom. I'd like to go to Fudan University if I have the chance!
# 创建一个名为survey_dict的空字典
survey_dict = {}
while 1:
print('If you have the chance, which university do you want to go to most?')
print('What is your name?')
# 将读取到的字符串存储在变量name中
name = input()
print('Which university do you want to go to most?')
# 将读取到的字符串存储在变量university中
university = input()
# 把键-值对name: university存储在字典survey_dict中
survey_dict[name] = university
print("Is there anyone who hasn't been investigated yet?")
result = input()
if result == 'No':
break
for name in sorted(survey_dict, reverse=False):
print(f"I'am {name}. I'd like to go to {survey_dict[name]} if I have the chance!")