【问题标题】:How to use the 'return' statement properly in Python如何在 Python 中正确使用“return”语句
【发布时间】:2017-08-29 23:34:38
【问题描述】:

我的代码中有一些重大错误,我不明白是什么原因造成的。我已经尝试更改我的退货声明的格式,但我一直收到同样的错误。

这是我当前的代码:

print("Kiran's Quiz: A quiz made by Kiran!\n")

def answers():
    points = 0
    x = input("Question 1: How far away is  the Earth from the Sun? Give your answer in 'n million miles'.")
    y = input("Question 2: What colour is white? Give your answer in 'x colour(s)'.")
    z = input("Question 3: What temperature is boiling water? Give your answer in 'n degrees centigrade'.")

    while (x != "93 million miles") or (y != "every colour") or (z != "100 degrees centigrade"):
        print ("You got it wrong. -1 point for you!.")
        points -= 1
        print("You have" + str(points) + ("points.")
    
    points += 1
    print("Hooray, you got it correct! +1 to you!")

    return x, y, z

answers()

我收到此错误:

Traceback (most recent call last):
  File "python", line 17
    return x, y, z
         ^
SyntaxError: invalid syntax

Code and error

【问题讨论】:

  • 在您的 while 循环中,您的第二个 print 缺少右括号 )
  • return 没有正确缩进
  • 另外,(points) - 1 什么也不做。你必须points -= 1
  • return 应该在def answer() 中。你在外面写。
  • 我上面的cmets中的每个人都是正确的。

标签: python-3.x return syntax-error


【解决方案1】:

错误:

  • print 语句缺少右括号
  • return 函数外

代码:

print("Kiran's Quiz: A quiz made by Kiran!\n")

def answers():
    points = 0
    x = input("Question 1: How far away is  the Earth from the Sun? Give your answer in 'n million miles'.")
    y = input("Question 2: What colour is white? Give your answer in 'x colour(s)'.")
    z = input("Question 3: What temperature is boiling water? Give your answer in 'n degrees centigrade'.")

    while (x != "93 million miles") or (y != "every colour") or (z != "100 degrees centigrade"):
        print ("You got it wrong. -1 point for you!.")
        points -= 1
        print("You have" + str(points) + ("points."))

    points += 1
    print("Hooray, you got it correct! +1 to you!")
    return (x, y, z)

answers()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2013-01-31
    • 2014-02-02
    • 1970-01-01
    相关资源
    最近更新 更多