第二个def的语法错误

时间:2013-01-24 04:23:51

标签: python python-2.7 syntax-error function

运行此代码时,我遇到了第二个def,* de * f multiply():当我收到语法错误时,de的def被删除了。

import random
def start () :

    print "Welcome!"
    choose ()

def choose () :

    choice = input """would you like to
     add, subtract, or multiply?
      1       2            3
    """
    if choice = 1 :
        add ()
    if choice = 2 :
        subtract ()
    if choice = 3 :
        multiply ()


def multiply () :

    x = random.random ()
    x = round ()
    y = random.random ()
    y = round ()
    print "What is the answer to: ", x,"*", y, " ?"
    answer = input ": "
    z = x*y
    if answer == z :
        print "you are correct!"
    elif answer < z :
        print "your answer is low! The correct answer was ", z
    elif answer > z :
        print "your answer is high! The correct answer was ", z
    multiply ()

def add () :

    x = random.random ()
    x = round ()
    y = random.random ()
    y = round ()
    print "What is the answer to: ", x,"+", y, " ?"
    answer = input ": "
    z = x+y
    if answer == z :
        print "you are correct!"
    elif answer < z :
        print "your answer is low! The correct answer was ", z
    elif answer > z :
        print "your answer is high! The correct answer was ", z

def subtract () :

    x = random.random ()
    x = round ()
    y = random.random ()
    y = round ()
    print "What is the answer to: ", x,"*", y, " ?"
    answer = input ": "
    z = x*y
    if answer == z :
        print "you are correct!"
    elif answer < z :
        print "your answer is low! The correct answer was ", z
    elif answer > z :
        print "your answer is high! The correct answer was ", z

2 个答案:

答案 0 :(得分:2)

input是一个函数,所以你必须像一个函数一样调用它:

input('Input some stuff: ')

你也有几行看起来像这样:

if choice = 1 :

你想写choice == 1。最后,这部分有点奇怪:

x = random.random ()
x = round ()

您可能希望将x传递给round

x = random.random ()
x = round (x)

或者只是完全跳过该部分并使用randint

x = random.randint(0, 1)

答案 1 :(得分:0)

以下是代码中的一些逻辑和语法错误:

answer = input ": "

您可以这样调用输入:

answer = input(": ")


if choice = 1 :

=是作业。你的意思是==。


x = random.random ()
x = round ()

如果你指定x是第一件事,那么第二件事,就像第一个任务从未发生过一样。您的意思是x = round(x)吗?