如何用脚本生成随机食谱

wen 实用脚本 2

本文目录导读:

如何用脚本生成随机食谱

  1. 思路说明
  2. 版本一:Python 控制台版(简单,直接运行)
  3. 版本二:HTML 网页版(可视化 + 可一键刷新)
  4. 如何扩展?

这是一个“随机食谱生成器”的实现思路,由于“随机食谱”需要兼顾可食用性(不能是石头+洗洁精)和趣味性,我准备了两个版本:一个是简单的控制台版,另一个是视觉上更丰富的网页版。

思路说明

随机食谱的核心是数据结构 + 随机组合,我们会定义不同的食材库(主料、辅料、调料、烹饪方法、菜系风格),然后从每个库中随机抽取一项进行组合。


Python 控制台版(简单,直接运行)

这个版本适合只想快速生成文字食谱的用户。

import random
# 定义食材和风格库
ingredients = {
    "主料": ["鸡胸肉", "三文鱼", "豆腐", "牛肉", "鸡蛋", "土豆", "虾仁", "意大利面"],
    "辅料": ["洋葱", "大蒜", "青椒", "番茄", "蘑菇", "西兰花", "胡萝卜", "菠菜"],
    "调料": ["生抽", "橄榄油", "黑胡椒", "咖喱粉", "椰奶", "番茄酱", "辣椒酱", "蜂蜜"],
    "烹饪法": ["清炒", "烤箱烘烤", "水煮后凉拌", "铁板煎", "慢炖", "油炸", "蒸", "烧烤"],
    "风格": ["中式", "意式", "泰式", "日式", "墨西哥式", "印度式", "地中海式"],
}
def generate_random_recipe():
    # 从每个类别中随机选一个
    main = random.choice(ingredients["主料"])
    side = random.choice(ingredients["辅料"])
    sauce = random.choice(ingredients["调料"])
    method = random.choice(ingredients["烹饪法"])
    style = random.choice(ingredients["风格"])
    # 组装成句子
    name = f"{style}风味{method}{main}"
    steps = [
        f"1. 将{main}处理干净,切成合适大小。",
        f"2. 将{side}切好备用。",
        f"3. 热锅,加入适量{sauce}。",
        f"4. 使用{method}的方式烹饪主料和辅料。",
        f"5. 最后加入{sauce}调味,出锅。",
    ]
    print("=" * 40)
    print(f"【今日随机食谱】: {name}")
    print("=" * 40)
    for step in steps:
        print(step)
    print(f"提示:这是一道{style}菜,建议搭配米饭或面包。\n")
if __name__ == "__main__":
    input("按回车键生成一道随机食谱...")
    generate_random_recipe()

运行效果示例:

按回车键生成一道随机食谱...
========================================
【今日随机食谱】: 泰式风味清炒虾仁
========================================
1. 将虾仁处理干净,切成合适大小。
2. 将西兰花切好备用。
3. 热锅,加入适量椰奶。
4. 使用清炒的方式烹饪主料和辅料。
5. 最后加入椰奶调味,出锅。
提示:这是一道泰式菜,建议搭配米饭或面包。

HTML 网页版(可视化 + 可一键刷新)

如果你希望得到更直观、视觉上更有“食谱感”的效果,可以复制以下代码保存为 .html 文件,在浏览器中打开。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">🍽️ 随机食谱生成器</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
            padding: 20px;
            box-sizing: border-box;
        }
        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.15);
            padding: 40px;
            max-width: 600px;
            width: 100%;
            text-align: center;
            transition: all 0.3s ease;
        }
        h1 {
            color: #e74c3c;
            font-size: 2.2em;
            margin-bottom: 30px;
            letter-spacing: 1px;
        }
        .recipe-card {
            background: #fef9ef;
            border-radius: 15px;
            padding: 25px;
            margin: 20px 0;
            border-left: 5px solid #e67e22;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        .recipe-name {
            font-size: 1.8em;
            color: #2c3e50;
            font-weight: bold;
            margin-bottom: 15px;
        }
        .recipe-detail {
            text-align: left;
            margin: 15px 0;
            line-height: 1.8;
            color: #555;
        }
        .recipe-detail span {
            font-weight: bold;
            color: #e67e22;
        }
        button {
            background: #e74c3c;
            color: white;
            border: none;
            padding: 15px 40px;
            border-radius: 50px;
            font-size: 1.2em;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(231, 76, 60, 0.3);
            margin-top: 10px;
        }
        button:hover {
            background: #c0392b;
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(231, 76, 60, 0.4);
        }
        .footer {
            margin-top: 25px;
            font-size: 0.8em;
            color: #aaa;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🍳 随机食谱</h1>
        <div class="recipe-card" id="recipeDisplay">
            <div class="recipe-name" id="recipeName">按一下按钮</div>
            <div class="recipe-detail" id="recipeDetails">
                <span>主料:</span><span id="mainIngredient">-</span><br>
                <span>辅料:</span><span id="sideIngredient">-</span><br>
                <span>调料:</span><span id="sauceIngredient">-</span><br>
                <span>烹饪法:</span><span id="cookingMethod">-</span><br>
                <span>风格:</span><span id="cuisineStyle">-</span>
            </div>
        </div>
        <button onclick="generateRecipe()">🎲 生成新食谱</button>
        <div class="footer">每次点击都会产生不同的搭配组合</div>
    </div>
    <script>
        // 数据池
        const data = {
            main: ["鸡胸肉", "三文鱼", "豆腐", "牛肉", "鸡蛋", "土豆", "虾仁", "意大利面", "羊排", "茄子"],
            side: ["洋葱", "大蒜", "青椒", "番茄", "蘑菇", "西兰花", "胡萝卜", "菠菜", "玉米", "甜椒"],
            sauce: ["生抽", "橄榄油", "黑胡椒", "咖喱粉", "椰奶", "番茄酱", "辣椒酱", "蜂蜜", "柠檬汁", "芝麻酱"],
            method: ["清炒", "烤箱烘烤", "水煮后凉拌", "铁板煎", "慢炖", "油炸", "蒸", "烧烤", "爆炒", "生食(需新鲜)"],
            style: ["中式", "意式", "泰式", "日式", "墨西哥式", "印度式", "地中海式", "法式", "韩式", "美式"]
        };
        function getRandomItem(arr) {
            return arr[Math.floor(Math.random() * arr.length)];
        }
        function generateRecipe() {
            // 随机选取
            const main = getRandomItem(data.main);
            const side = getRandomItem(data.side);
            const sauce = getRandomItem(data.sauce);
            const method = getRandomItem(data.method);
            const style = getRandomItem(data.style);
            // 更新显示
            document.getElementById('recipeName').textContent = `${style}风味${method}${main}`;
            document.getElementById('mainIngredient').textContent = main;
            document.getElementById('sideIngredient').textContent = side;
            document.getElementById('sauceIngredient').textContent = sauce;
            document.getElementById('cookingMethod').textContent = method;
            document.getElementById('cuisineStyle').textContent = style;
            // 添加一点微小的动画效果(可选)
            const card = document.querySelector('.recipe-card');
            card.style.transition = '0.1s';
            card.style.transform = 'scale(0.98)';
            setTimeout(() => {
                card.style.transform = 'scale(1)';
            }, 100);
        }
        // 页面加载时自动生成一次
        window.onload = generateRecipe;
    </script>
</body>
</html>

网页版特点:

  • 按下按钮即可生成新的随机食谱。
  • 显示更细致:主料、辅料、调料、做法、风格分开展示。
  • 界面美观,适合分享。

如何扩展?

这两个脚本都预留了扩展接口,如果你想生成更“真实”的食谱,可以:

  1. 增加数据库:把食材列表换成真实的、可搭配的食材(三文鱼”通常配“柠檬”而不是“辣椒酱”)。
  2. 添加权重:某些搭配更合理(如“日式”配“照烧酱”概率更高)。
  3. 生成完整步骤:而不是通用步骤。
  4. 集成图像:使用 Unsplash API 或者本地图片库显示对应的食物图片。
  • 最简单的:复制 Python 代码,本地运行。
  • 想要好看的:复制 HTML 代码,保存为 .html 文件打开。
  • 核心逻辑都是从列表中随机抽取 + 字符串拼接

试试看,你会发现有些随机组合非常“黑暗料理”,而有些意外地让人有食欲!

抱歉,评论功能暂时关闭!