0

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

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
4

2 回答 2

2

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

input('Input some stuff: ')

您还有几行如下所示:

if choice = 1 :

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

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

您可能想要传递xround

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

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

x = random.randint(0, 1)
于 2013-01-24T04:30:20.747 回答
0

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

answer = input ": "

你这样调用输入:

answer = input(": ")


if choice = 1 :

= 是赋值。你的意思是==。


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

如果您将 x 分配为第一件事,然后是第二件事,就好像第一次分配从未发生过。你的意思是x = round(x)


于 2013-01-24T04:31:52.350 回答