gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:23:42 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com