菜谱生成小游戏:多种制作方式(如:清蒸,红烧…)和多种菜品(如:;牛肉,鲤鱼…),利用Python列表类型编写程序组合生成菜谱
时间: 2024-01-14 10:04:58 浏览: 36
好的,这个小游戏的制作可以分为以下几个步骤:
1. 定义菜品和制作方式
我们可以先定义一个包含多种菜品的列表和一个包含多种制作方式的列表,例如:
```
dishes = ['牛肉', '鲤鱼', '烤鸭', '水煮鱼']
cooking_methods = ['清蒸', '红烧', '煎', '炖']
```
2. 随机生成菜品和制作方式
利用Python的`random`模块,我们可以随机生成一个菜品和一个制作方式,例如:
```
import random
dish = random.choice(dishes)
cooking_method = random.choice(cooking_methods)
print('今天的菜谱是:{}{}'.format(cooking_method, dish))
```
3. 多次生成不重复的菜谱
如果我们想生成多个不重复的菜谱,可以使用Python的`set`集合类型来存储已经生成过的菜谱,例如:
```
recipes = set()
while len(recipes) < 5:
dish = random.choice(dishes)
cooking_method = random.choice(cooking_methods)
recipe = '{}{}'.format(cooking_method, dish)
recipes.add(recipe)
print('今天的菜谱有:')
for recipe in recipes:
print(recipe)
```
上述代码中,我们使用了`while`循环来生成5个不重复的菜谱,并使用`set`类型来确保每个菜谱只出现一次。
综合起来,这个小游戏的完整代码如下:
```
import random
dishes = ['牛肉', '鲤鱼', '烤鸭', '水煮鱼']
cooking_methods = ['清蒸', '红烧', '煎', '炖']
# 随机生成一个菜谱
dish = random.choice(dishes)
cooking_method = random.choice(cooking_methods)
print('今天的菜谱是:{}{}'.format(cooking_method, dish))
# 随机生成5个不重复的菜谱
recipes = set()
while len(recipes) < 5:
dish = random.choice(dishes)
cooking_method = random.choice(cooking_methods)
recipe = '{}{}'.format(cooking_method, dish)
recipes.add(recipe)
print('今天的菜谱有:')
for recipe in recipes:
print(recipe)
```
你可以根据自己的需求修改菜品和制作方式的列表,来生成不同的菜谱。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)