【问题标题】:Expected Expression Error in my Python script我的 Python 脚本中的预期表达式错误
【发布时间】:2022-11-18 13:03:38
【问题描述】:

我在 Visual Studio 上尝试更新我的 Itch Quiz Game,当我尝试测试一个带有打印函数的 Else 函数时,它说我有一个 Expected Expression 错误。我试图修复它,但没有用。请帮忙

这是我的代码

import time


print("Math Game")
print("")

score = 0
    #intro 
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":
    else: ---THIS IS THE ERROR
    print("Test")
    
    
    score += 1

    #Question 2
print("Whats 4 * 2")
answer2 = input("Enter answer")

if answer2 == "8":
    score += 1

        #Question 3
print("Whats the root square 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("Whats Apples frist device the Phone or the Computer")
answer4 = input("Enter answer")

if answer4 == "Computer":
    score += 1

    print("you got " + str(score) + "/5")
    time.sleep(5)
print("Good Bye!")

【问题讨论】:

  • 您缺少 if 语句的正文。并且 else: 需要与匹配的 if 处于相同的缩进级别。
  • 顺便说一句,你拼错了“答案”。
  • 你应该使用 score += 1 而不是 else:。像所有其他问题一样。

标签: python


【解决方案1】:

主要问题是Question 1 中的if-else 语句的主体是空的。你需要在每个if/else statement中至少有一行代码。如果什么都不做,可以使用关键字pass

if answer1 == "4":
    # ...
    score += 1 # this should go here, when answer1=='4', right?
    # ...
else: 
    pass

https://www.w3schools.com/python/ref_keyword_pass.asp


此外,if-else 语句的所有子句都应具有相同的缩进级别。

【讨论】:

  • 这是有道理的,非常感谢你
  • 我很高兴能提供帮助。如果答案有用请点赞。
【解决方案2】:

这违反了 if-else 规则。 可以这样直接pass。

if answer1 == "4":
    score += 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多