You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python新手编写Madlib游戏遇语法错误求助

Python Madlib游戏语法错误修正

你的代码存在多个基础语法问题,逐个修正如下:

主要错误点

  • 函数定义不规范:缺少def关键字,且函数体未缩进。正确的函数定义应为def madlib():,函数内代码需缩进4个空格。
  • 变量名非法:Python变量名不能包含空格,first name、second pronoun这类名称要改成下划线连接的形式,比如first_name、second_pronoun。
  • input提示文本错误:input的参数是提示用户输入的文字,不是示例值,应写成input("请输入一个代词:")这类格式。
  • print语法错误:Python3中print是函数必须加括号;字符串与变量拼接时,原代码直接堆砌无分隔,可用逗号分隔(print会自动加空格)或f-string格式化更简洁。
  • 变量名与拼写错误:place 2是无效变量名,需改为place2;temperatrue要修正为temperature,2os改为20s。
  • 未调用函数:定义完madlib函数后,需调用madlib()才能执行代码。

修正后的完整代码

def madlib():
    print("welcome to my mad lib")

    pronoun1 = input("请输入一个代词(比如fans):")
    first_name = input("请输入名字(比如jeremy):")
    noun1 = input("请输入一个名词(比如tree):")
    last_name = input("请输入姓氏(比如thomas):")
    second_pronoun = input("请输入第二个代词(比如stars):")
    place = input("请输入地名(比如broward):")
    third_pronoun = input("请输入第三个代词(比如lions):")
    place2 = input("请输入第二个地名(比如LA):")
    fourth_pronoun = input("请输入第四个代词(比如moons):")
    noun2 = input("请输入第二个名词(比如chair):")
    adjective1 = input("请输入形容词(比如sharp):")
    adjective2 = input("请输入第二个形容词(比如smelly):")
    verb = input("请输入动词(比如fighting):")
    adjective3 = input("请输入第三个形容词(比如tall):")

    # 使用f-string格式化输出,更清晰易读
    print(f"hello there, sports {pronoun1}, this is {first_name} talking to you from the press {noun1} in {last_name} stadium, where 57,000 cheering {second_pronoun} have gathered to watch the {place} {third_pronoun} take on the {place2} {fourth_pronoun}. Even though the {noun2} is shining, it's a {adjective1} cold day with the temperature in the {adjective2} 20s. We'll be back for the opening {verb} after a few words from our {adjective3} sponsor.")

# 调用函数执行
madlib()

内容的提问来源于stack exchange,提问作者jeremyt123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 15:35:14