我的程序接受一个数学输入并在继续之前检查它是否有错误,下面是我需要帮助的部分代码:expression= introduction()#just asks user to input a math expression
operators= set("*/+-")
numbers= set("0123456789")
for i in expression:
while i not in numbers and i not in operators:
print("Please enter valid inputs, please try again.")
expression= introduction()
现在我已经设置了一个错误循环,但是我遇到的问题是,我不知道在这个场景中用什么来更新循环。有人吗?在
我需要一些简单的东西。我需要一些与本篇文章中发布的代码相近的东西,比如:
^{pr2}$
请注意,此代码也不起作用。它会对用户输入的“表达式”中的每一个错误进行循环。在
下面这些东西太先进了,我无法使用它们:valid = operators | numbers
while True:
expression = introduction()
if set(expression) - valid:
print 'not a valid expression, try again'
else:
breakimport string
expression = introduction()
operators = {'*', '/', '+', '-'}
numbers = set(string.digits)
while any(char not in numbers and char not in operators for char in expression):
print("Please enter valid inputs, please try again.")
expression = introduction()