我看到这个代码有几个不同的问题。不过没有什么不能修复的!
首先,注意间距/缩进。 Python使用它来知道如何“读”你的程序,并告诉他们哪些代码行与其他代码。我敢肯定,在这里一些有用的用户编辑您的原始q将有适当的缩进:-)
def rep(num1,ans):
num2 = int(input("Next number? "))
choice = input("select operation -,+,x,/. ")
if (choice == "+"):
ans = (num1+num2)
elif (choice == "-"):
ans = (num1-num2)
elif (choice == "x"):
ans = (num1*num2)
elif (choice == "/"):
ans = (num1/num2)
print (ans)
num1 = int(input("First number? "))
rep(num1, ans)
这是你的第一个代码块,在其中定义rep
功能与一些固定的间距。问题在于,你在传递ans
时没有定义它。只是将其删除作为参数,而是已经ans
获得由rep
返回:
def rep(num1):
num2 = int(input("Next number? "))
choice = input("select operation -,+,x,/. ")
if (choice == "+"):
ans= (num1+num2)
elif (choice == "-"):
ans= (num1-num2)
elif (choice == "x"):
ans= (num1*num2)
elif (choice == "/"):
ans= (num1/num2)
return ans
num1 = int(input("First number? "))
rep(num1)
开,2号地块:
morenum = ("yes")
morenum = input("Do you want to use another number? ")
while (morenum == "yes"):
rep(num1)
这是你的代码变得有点靠不住。您最初定义了morenum
变量,然后将其替换为输入。此外,这不会按照您认为的方式工作 - 我认为您的印象是,这会询问用户他们是否想在之后想要另一个号码,然后他们每次都完成数学运算。为了实现这一点,使用此代码来代替:
while True:
morenum = input("Do you want to use another number? ")
if morenum == 'yes':
num1 = int(input("First number? "))
rep(num1)
else:
quit()