2017-04-04 50 views
-3

我在第6年,我正在编写一个Python测验(只是为了好玩),而且我陷入了一个主要问题。 '返回声明不起作用。 这是我当前的代码:如何在Python中正确使用'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() 

和我收到此错误:

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

综上所述,我需要知道“return”语句是如何工作的 - 正常 - 如果你有时间,请看看旧版本的我的代码。任何帮助,一如既往,将不胜感激!

+0

的In y我们的'while'循环,你的第二个'print'缺少一个右括号')'。 – zondo

+0

'return'没有正确缩进 –

+0

另外,'(points) - 1'什么都不做。你必须'points - = 1' – Li357

回答

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() 
+0

感谢您的回答Farhadur Ra​​ja Fahim,这是每个问题都应该有的! – Kiran