python - 第二个def上的语法错误

标签 python python-2.7 syntax-error function

运行此代码时,我遇到第二个def的麻烦,* de * f乘法():当我收到语法错误时,就会发出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

最佳答案

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)

关于python - 第二个def上的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14493949/

相关文章:

python-3.x - Python 3,初学者错误

ms-access - 使用存储过程的查询表达式中的语法错误(缺少运算符)

python - Dask DataFrame 计算多列分组内的平均值

python - 即使 "if var:"根本不存在,怎么说 "var"?

python - networkx 从 python 字典设置节点属性

python - 检查对象(具有某些属性值)是否不在列表中

timestamp - 将CURRENT_TIMESTAMP分配给TIMESTAMP字段时,plpgsql语法错误

python - 如何获得由 zope.transaction 管理的 SQLAlchemy session ,它具有与 http 请求相同的范围但不会在提交时自动关闭?

python - python中的文本文件

python - pytest 以及为什么要避免初始化文件