我正在尝试在我的计算器中创建退出功能,我尝试了很多方法,但它不起作用。我不明白如何循环我的代码,没有网站解释该怎么做。
这是我的代码:
#Variables:
about = (" + Addition, - Subtraction, * Multiplication, / Division, ** Exponent, % Remainder")
print( )
print(about)
print( )
#Functions:
#Returns the sum of num1 + num2
def add(num1, num2):
return num1 + num2
#Returns the difference of num1 - num2
def sub(num1, num2):
return num1 - num2
#Returns the product of num1 * num2
def mul(num1, num2):
return num1 * num2
#Returns the quotient of num1 / num2
def div(num1, num2):
return num1 / num2
#Returns the power of num1 ** num2
def px(num1, num2):
return num1 ** num2
#Returns the remainder of num1 % num2
def rem(num1, num2):
return num1 % num2
#Main area where all the magic happens
#Wrong operation
def main():
operation = input("What do you need me for? (+,-,*,/,**,%): ")
if(operation != '+' and operation != '-' and operation != '*'
and operation != '/' and operation != '**' and operation != '%'):
#invalid operation
print("Sorry, but that's not an operation")
#What the operations should do
else:
var1 = int(input("Enter num1: "))
var2 = int(input("Enter num2: "))
if(operation == '+'):
print(round(add(var1, var2), 2))
elif(operation == '-'):
print(round(sub(var1, var2), 2))
elif(operation == '*'):
print(round(mul(var1, var2), 2))
elif(operation == '/'):
print(round(div(var1, var2), 2))
elif(operation == '**'):
print(round(px(var1, var2), 2))
elif(operation == '%'):
print(round(rem(var1, var2), 2))
#Exit function
close = input ("Do you want to exit?: ")
if(close == 'Yes' and close == 'yes'):
close()
elif(close == 'No' and close == 'no'):
main()
main()
有人可以向我解释为什么它不起作用吗?
答案 0 :(得分:1)
close
无法同时为'No'
和'no'
,因此始终为假。
所以你可能意味着close == 'No' or close == 'no'
。但更好的方法是close.lower() == 'no'
你正在做的不是循环而是递归(函数main
调用自身)。如果你需要循环,那么试试这个:
while True:
# your code
close = input ("Do you want to exit?: ")
if close.lower() == 'yes':
break; # escapes the loop
答案 1 :(得分:0)
将main()
函数替换为以下函数:
def main():
close = 'no'
while close.lower() == 'no':
print "inside while"
operation = input("What do you need me for? (+,-,*,/,**,%): ")
if operation not in ['+', '-', '*', '/', '**', '%']:
print("Sorry, but that's not an operation")
else:
var1 = int(input("Enter num1: "))
var2 = int(input("Enter num2: "))
if(operation == '+'):
print(round(add(var1, var2), 2))
# ...
elif(operation == '%'):
print(round(rem(var1, var2), 2))
close = input ("Do you want to exit?: ")
close
初始化为'no'
,因此循环至少执行一次。
您可以使用close == 'no' or close == 'No'
代替lower()
而不是close.lower() == 'no'
您应该使用:
if operation not in ['+', '-', '*', '/', '**', '%']:
而不是:
operation != '+' and operation != '-' and operation != '*' ...
答案 2 :(得分:0)
即使用户说'是'或只是'y'
,我也想退出while True:
operation = input("What do you need me for? (+,-,*,/,**,%): ")
if operation not in ['+', '-', '*', '/', '**', '%']:
print("Sorry, but that's not an operation")
continue
....
close = input ("Do you want to exit?: ")
if close and close[0].lower() == 'y':
print('Bye')
return
当用户只需按Enter键时,它也应该处理空字符串。
答案 3 :(得分:0)
变量close不能同时为2个字符串。如果您不想更改代码,我建议您更改:if close == "Yes" and "yes":
至if close == "Yes "or "yes"