我遇到了让while循环工作的问题。我使用next = y命令来启动程序,但是在我编写的结尾(next == y)再次运行main函数时,这个位似乎不起作用。即使我输入'n'或任何不是'y'的东西,函数仍然会重复。我的想法是,最初的next = y覆盖了所有内容,但我似乎无法删除此代码,否则代码只会运行并且没有任何代码运行。为什么我的while循环不起作用
next = "y"
def main():
operator = input("Select a function and press enter (+, - , *, /) ")
if(operator != "+" and operator != "-" and operator != "*"and operator >!= "/"):
print(input("You must enter a valid operator "))
else:
val1 = int(input("Select value 1 "))
val2 = int(input("Select value 2 "))
if(operator == "+"):
print(add(val1, val2))
elif(operator == "-"):
print(sub(val1, val2))
elif(operator == "*"):
print(mult(val1, val2))
else:
print(div(val1, val2))
next = (input("Would you like to do another calculation? (y/n): "))
while(next == "y"):
main()
我认为这是一个简单的修复,但我不知道如何做到这一点。
能否请你纠正代码段的缩进?很难理解“main”和“outside”之外的内容。 – FlipTack