print('Select operation')
print('Choose from:')
print('+')
print('-')
print('*')
print('/')
choice=input('Enter choice (+,-,*,/):')
num1=int(input('Enter first number:'))
num2=int(input('Enter second number:'))
if choice== '+':
print(num1,'+',num1,'=', (num1+num2))
while restart **=** input('Do you want to restart the calculator y/n'):
if restart == 'y':t
print('restart')
else restart == 'n':
print('Thanks for using my program')
break
elif choice== '-':
print(num1,'-',num2,'=', (num1-num2))
elif choice== '*':
print(num1,'*',num2,'=', (num1*num2))
elif choice== '/':
print(num1,'/',num2,'=',(num1/num2))
else:
print('Invalid input')
=粗体有什么问题?我不明白它有什么问题?有人请回答我的问题。
谢谢你, 夏洛特
答案 0 :(得分:0)
您已尝试将赋值语句用作布尔值;这失败了几个方面。最重要的是,您将restart
逻辑分散到多行代码中,并使解析器混乱。
你可能想要这样的东西:
restart = input('Do you want to restart the calculator y/n')
while restart.lower() == 'y':
...
restart = input('Do you want to restart the calculator y/n')
答案 1 :(得分:0)
这里有多个问题:
无法在while循环中检查从input
到变量的赋值,您应该将其拆分为赋值并进行检查。
else
不能包含条件
您在打印结果时遇到错误 - 您打印num1
两次
缩进在Python中很有意义 - 请确保下次正确缩进
解决上述问题:
def calc():
print('Select operation')
print('Choose from:')
print('+')
print('-')
print('*')
print('/')
choice=input('Enter choice (+,-,*,/):')
num1=int(input('Enter first number:'))
num2=int(input('Enter second number:'))
if choice == '+':
print("{}+{}={}".format(num1, num2, num1+num2))
elif choice == '-':
print("{}-{}={}".format(num1, num2, num1-num2))
elif choice == '*':
print("{}*{}={}".format(num1, num2, num1*num2))
elif choice == '/':
print("{}/{}={}".format(num1, num2, num1/num2))
else:
print('Invalid input')
if __name__ == '__main__':
restart = 'y'
while restart:
if restart == 'y':
print('restart')
calc()
restart = input('Do you want to restart the calculator y/n')
elif restart == 'n':
print('Thanks for using my program')
break