Why is it saying that the = in bold is an invalid syntax?
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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') |
=粗体有什么问题? 我不明白它有什么问题? 有人请回答我的问题。
谢谢,
夏洛特
这里有多个问题:
无法在while循环中检查从
你也有打印结果的错误 - 你打印
缩进在Python中很有意义 - 请确保下次正确缩进
修复上述问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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 |
您已尝试将赋值语句用作布尔值; 这失败了几个方面。 最重要的是,你将
你可能想要这样的东西:
1 2 3 4 | 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') |