如何使用' return'在Python中正确声明

时间:2017-04-04 16:28:38

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

我在第6年,我正在编写一个Python测验(仅仅是为了好玩)而且我遇到了一个重大问题。退货声明不起作用。 这是我目前的代码:

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 1

另外,如果你有足够的时间,这是我的代码的旧版本:

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

def answers(x, y, z):
    points = 0
    x = input("Question 1: How far away is  the Earth from the Sun?")
    y = input("Question 2: What colour is white?")
    z = input("Question 3: What temperature is boiling water?")

    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(x, y, z)

我一直接受这个,奇怪的错误:

Traceback (most recent call last):
    File "python", line 16
        print("Horray, you got it correct! +1 to you!")
            ^
SyntaxError: invalid syntax

Code and error 2

总结一下,我需要知道'返回'声明正常 - 如果你有时间,请查看旧版本的代码。任何帮助,一如既往,将不胜感激!

1 个答案:

答案 0 :(得分:0)

<强>错误:

  • 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()