包含if语句的行语法错误

时间:2016-05-09 08:07:46

标签: python python-2.7 python-3.x

我一直试图简单地猜测数字游戏,但Python一直在说我的代码有语法错误。我做错了什么?

import random
takeone = input("Guess A Number Between 1 & 5")
numberinit = random.randint(1,5)
if "takeone" == "numberinit"
    print("Your Right")
else:
    print("Your Wrong")

2 个答案:

答案 0 :(得分:2)

您获得的语法错误可能类似于以下内容:

  File "test.py", line 4
    if "takeone" == "numberinit"
                               ^    
SyntaxError: invalid syntax

这意味着您在if行的末尾错过了一个冒号。该行应改为:

if "takeone" == "numberinit":

答案 1 :(得分:1)

有关Python if语句的文档可以在https://docs.python.org/2/tutorial/controlflow.html找到。

这是一个正确的例子:

import random
takeone = input("Guess A Number Between 1 & 5")
numberinit = random.randint(1,5)
if "takeone" == "numberinit":
    print("Your Right")
else:
    print("Your Wrong")