大佬教程收集整理的这篇文章主要介绍了为什么如果不是操作 == '/' 或 '*' 或 '+' 或 '-':给我一个语法错误,,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
if not operation == '/' or '*' or '+' or '-':
print('not a valID answer,try again')
operation = (input('Please enter what operation you would like to do,/ is divIDe,* is multiply,+ is plus and - is minus')
operation = (input('Please enter what operation you would like to do,/ is divide,* is multiply,+ is plus and - is minus')
您忘记添加第二个右大括号。在这种情况下,只需移除左括号。
此外,您的 if
语句将始终为 True。以下代码将起作用:
if not operation in ['/','*','+','-']: # test if operation is one of /,*,+ and -
print('not a valid answer,try again')
operation = input('Please enter what operation you would like to do,+ is plus and - is minus')
,
你的意思是这样吗?请注意更正后的语法和逻辑。使用无限循环,提示输入用户输入之前检查该输入。当 break
属于允许值集时退出循环 (operation
)。否则,重复循环并再次提示用户输入。使用 f-strings or formatted string literals 打印不正确的输入(使用户错误更容易看到)。
while True:
operation = input('Please enter what operation you would like to do,+ is plus and - is minus: ')
if operation in {'/','-'}:
break
print(f'Not a valid answer: {operation},try again')
,
被 or
语句分解的每个子句都是它自己的布尔值。
所以 if not operation == '/' or '*' or '+' or '-':
实际上代表四种不同的布尔值:
if not operation == '/'
'*'
'+'
'-'
要纠正此问题,您需要将每个语句编写为自己的条件:
if not operation == '/'
if not operation == '*'
if not operation == '+'
if not operation == '-'
或者,串在一起,
if not operation == '/' or not operation == '*' or not operation == '+' or not operation == '-'
以上是大佬教程为你收集整理的为什么如果不是操作 == '/' 或 '*' 或 '+' 或 '-':给我一个语法错误,全部内容,希望文章能够帮你解决为什么如果不是操作 == '/' 或 '*' 或 '+' 或 '-':给我一个语法错误,所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。