Python脚本Expected Expression Error问题求助(VS开发Itch问答游戏)
问题分析与修复
你遇到的Expected Expression错误来自于Question 1处的if-else语法不规范,具体问题如下:
if answer1 == "4":后面没有任何执行代码,直接跳转else,Python要求if分支必须有至少一条语句(哪怕是占位的pass)else:的缩进和对应的if没有对齐,违反Python的缩进规则- 后续的
print("Test")和score +=1缩进错误,没有正确归属到对应的分支里
修复后的完整代码如下:
import time print("Math Game") print("") score = 0 # 游戏介绍 print("Welcome to 5 Questions") answer6 = input("Type Begin to Begin: ") if answer6 == "Begin": score += 0 # Question 1 print("Whats 2 + 2?") answer1 = input("Enter answer: ") if answer1 == "4": score += 1 else: print("Test") # Question 2 print("Whats 4 * 2?") answer2 = input("Enter answer: ") if answer2 == "8": score += 1 # Question 3 print("Whats the square root of 16?") answer3 = input("Enter answer: ") if answer3 == "4": score += 1 # Question 4 print("Who made the laws of gravity?") answer4 = input("Enter answer: ") if answer4 == "Issac Newton": score += 1 # Question 5 print("What was Apple's first device: the Phone or the Computer?") answer5 = input("Enter answer: ") if answer5 == "Computer": score += 1 print(f"you got {score}/5") time.sleep(5) print("Good Bye!")
额外调整说明:
- 统一了所有注释的缩进,让代码结构更清晰
- 修正了Question 5的变量名(原代码重复使用了
answer4) - 把分数输出改成了更简洁的f-string写法
- 给所有输入提示加了冒号,提升用户体验
内容的提问来源于stack exchange,提问作者Bad Dev
相关产品推荐
相关产品推荐

