我有个编码问题要解决。我做了一半,但我想不出休息的时间。
以下是问题所在:
你有一群朋友来参加你的高中聚会,你想带他们去当地的一家餐馆吃饭。你不知道他们是否有饮食限制,但你的餐厅选择如下:乔的美食汉堡-素食主义者:不,维甘:不,无麸质:不
主街比萨公司-素食:是的,织女星:不,无麸质:是的
街角咖啡厅-素食主义者:是的,维根:是的,无麸质:是的
妈妈的意大利菜-素食主义者:是的,维甘:不,无麸质:不
素食主义者:是的,素食主义者:是的,无麸质:是的,写一个程序,询问你的聚会中是否有素食、素食或无麸质,然后只展示你可以带他们去的餐馆。
下面是程序输出的一个示例:
Is anyone in your party a vegetarian ? yes
Is anyone in your party a vegan ? no
Is anyone in your party gluten-free ? yes
Here are your restaurant choices:
Main Street Pizza Company
Corner Cafe
The Chef's Kitchen下面是程序输出的另一个示例:
Is anyone in your party a vegetarian ? yes
Is anyone in your party a vegan ? yes
Is anyone in your party gluten-free ? yes
Here are your restaurants choices:
Corner Cafe
The Chef's Kitchen下面是我到目前为止编写的代码:
# Get the status of a party
vegetarian = input('Is anyone in your party a vegetarian(yes/no)? ')
vegan = input('Is anyone in your party a vegan(yes/no)? ')
gluten_free = input('Is anyone in your party gluten-free(yes/or)? ')
# Assign restaurants based on the status of a party
if vegetarian == 'yes' and vegan == 'yes' and gluten_free == 'yes':
print("Here are your restaurant choices: \n"
"Corner Cafe \n"
"The Chef's Kitchen")
elif vegetarian == 'no' and vegan == 'no' and gluten_free == 'no':
print("Here are your restaurant choices: \n"
"Joe's Gourmet Burgers")这是输出:
Is anyone in your party a vegetarian(yes/no)? yes
Is anyone in your party a vegan(yes/no)? yes
Is anyone in your party gluten-free(yes/or)? yes
Here are your restaurant choices:
Corner Cafe
The Chef's KitchenIs anyone in your party a vegetarian(yes/no)? no
Is anyone in your party a vegan(yes/no)? no
Is anyone in your party gluten-free(yes/or)? no
Here are your restaurant choices:
Joe's Gourmet Burgers我想要的是写一份声明,根据答案随机选择餐馆。
但我能想出办法。
有人能告诉我怎么做吗?
我没有谷歌。我想学习。不是抄袭。提前谢谢你。
发布于 2021-12-10 10:12:56
preferences = []
d = {"yes": True, "no": False}
# fill up company preferences
preferences.append(d[input("Is anyone in your party a vegetarian(yes/no)? ")])
preferences.append(d[input("Is anyone in your party a vegan(yes/no)? ")])
preferences.append(d[input("Is anyone in your party gluten-free(yes/or)? ")])
restaurants = { # initialize a dictionary of all restaurants
"Joe's Gourmet Burgers": [False, False, False],
"Main Street Pizza Company": [True, False, True],
"Corner Cafe": [True, True, True],
"Mama's Fine Italian": [True, False, False],
"The Chef's Kitchen": [True, True, True]
}
print("Here are your restaurant choices:")
for restaurant, peculiarities in restaurants.items(): # iterate restaurants
if all(map(lambda x: x[0] == x[1] or not x[1], zip(peculiarities, preferences))): # if a restaurant is good for company print it
print(restaurant)发布于 2021-12-10 10:33:19
OOP:用属性将餐馆建模为类实例。提供有意义的str()。
按原样输入,循环遍历Restaurant实例列表,并将可能的内容放入列表中。
从列表中随机选择一个:
class Restaurant:
# allow mostly named params, no positional ones
def __init__(self, name, *, vegetarian=False, vegan=False, gluten_free=False):
self.name = name
self.vegetarian = vegetarian
self.vegan = vegan
self.gluten_free = gluten_free
def __str__(self):
s = [ k[0] for k in zip( ["Vegetarian", "Vegan", "Gluten free"],
(self.vegetarian, self.vegan, self.gluten_free)) if k[1]]
return f"{self.name} {'(' if s else ''}{', '.join(s)}{')' if s else ''}" 使用类的程序:
restaurants = [
Restaurant("Joe's Gourmet Burgers"),
Restaurant("Main Street Pizza Company", vegetarian=True),
Restaurant("Corner Cafe", vegetarian=True, vegan=True, gluten_free=True),
Restaurant("Mama's Fine Italian", vegetarian=True),
Restaurant("The Chef's Kitchen", vegetarian=True, vegan=True, gluten_free=True),
]
vegetarian = input('Is anyone in your party a vegetarian(y=yes)? ').strip().lower()[0] == "y"
vegan = input('Is anyone in your party a vegan(y=yes)? ').strip().lower()[0] == "y"
gluten_free = input('Is anyone in your party gluten-free(y=yes)? ').strip().lower()[0] == "y"
print()
all_r = []
for r in restaurants:
# vegetarians can eat vegan, meat lovers can eat vegetarian or vegan
if (not gluten_free or r.gluten_free) and (not vegan or r.vegan) and (not vegetarian or r.vegetarian or r.vegan):
print(r)
all_r.append(r) # add to list for random choice
import random
print("\nWe decided on: ", random.choice(all_r)) # choose one from list random输出2次运行:
Is anyone in your party a vegetarian(y=yes)? n
Is anyone in your party a vegan(y=yes)? n
Is anyone in your party gluten-free(y=yes)? n
Joe's Gourmet Burgers
Main Street Pizza Company (Vegetarian)
Corner Cafe (Vegetarian, Vegan, Gluten free)
Mama's Fine Italian (Vegetarian)
The Chef's Kitchen (Vegetarian, Vegan, Gluten free)
We decided on: Joe's Gourmet Burgers
Is anyone in your party a vegetarian(y=yes)? y
Is anyone in your party a vegan(y=yes)? n
Is anyone in your party gluten-free(y=yes)? y
Corner Cafe (Vegetarian, Vegan, Gluten free)
The Chef's Kitchen (Vegetarian, Vegan, Gluten free)
We decided on: The Chef's Kitchen (Vegetarian, Vegan, Gluten free)https://stackoverflow.com/questions/70302674
复制相似问题