运行此代码时,我遇到第二个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/