如何检查用户输入的操作数是否有效?(蟒蛇)

How can I check if the user has entered a valid operand? (Python)

提问人:Ant753 提问时间:11/13/2023 更新时间:11/13/2023 访问量:48

问:

我正在编写代码以在 python 中实现一个简单的计算器。我正在尝试检查用户是否输入了有效的操作数。我输入的代码如下。

calc = False
while calc == False:
    firstNum = 0
    operand = ''
    secondNum = 0
    firstNum = input('Please enter a number: ')
    if firstNum.isdigit() == False:
        print('Please enter a number.')
        continue
    else:
        firstNum = int(firstNum)
    operand = input('Please enter an operand (+, -, *, or /): ')
    if operand != '/' or operand != '*' or operand != '+' or operand != '-':
        print('Please enter a valid operand.')
        continue
    secondNum = input('Please enter another number: ')
    if secondNum.isdigit() == False:
        print('Please enter a number.')
        continue
    secondNum = int(secondNum)
    if secondNum == 0 and operand == '/':
        print('You cannot divide by 0. Please try again.')
        continue
    if operand == '+':
        calc = True
        print(f'{firstNum} + {secondNum} = {firstNum + secondNum}')
    elif operand == '*':
        calc = True
        print(f'{firstNum} * {secondNum} = {firstNum * secondNum}')
    elif operand == '-':
        calc = True
        print(f'{firstNum} - {secondNum} = {firstNum - secondNum}')
    else:
        calc = True
        print(f'{firstNum} / {secondNum} = {firstNum / secondNum}')
calc = False
while calc == False:
    firstNum = 0
    operand = ''
    secondNum = 0
    listOfOperands = ['+', '-', '/', '*']
    firstNum = input('Please enter a number: ')
    if firstNum.isdigit() == False:
        print('Please enter a number.')
        continue
    else:
        firstNum = int(firstNum)
    operand = input('Please enter an operand (+, -, *, or /): ')
    if operand != listOfOperands:
        print('Please enter a valid operand.')
        continue
    secondNum = input('Please enter another number: ')
    if secondNum.isdigit() == False:
        print('Please enter a number.')
        continue
    secondNum = int(secondNum)
    if secondNum == 0 and operand == '/':
        print('You cannot divide by 0. Please try again.')
        continue
    if operand == '+':
        calc = True
        print(f'{firstNum} + {secondNum} = {firstNum + secondNum}')
    elif operand == '*':
        calc = True
        print(f'{firstNum} * {secondNum} = {firstNum * secondNum}')
    elif operand == '-':
        calc = True
        print(f'{firstNum} - {secondNum} = {firstNum - secondNum}')
    else:
        calc = True
        print(f'{firstNum} / {secondNum} = {firstNum / secondNum}')

代码的当前版本始终拒绝操作数并返回到 while 循环的顶部。如果用户没有输入 +、-、* 或 /,我想返回到循环的顶部,如果他们输入了四个字符之一,我想继续完成循环。

python 验证 while-loop 布尔 用户输入

评论

0赞 Joe 11/13/2023
您可以将所有 s 更改为 s in ,或者更好的是检查四个操作数的元组中的成员资格。orandoperand != '/' or operand != '*' or operand != '+' or operand != '-'
0赞 Joe 11/13/2023
对于第二个版本,您需要更改为operand != listOfOperandsoperand not in listOfOperands
0赞 Ant753 11/13/2023
非常感谢!这很完美。
0赞 Joe 11/13/2023
别客气。请务必研究其中的区别,以便了解为什么新代码有效,而旧代码无效。

答:

0赞 Reilas 11/13/2023 #1

"...我正在尝试检查用户是否输入了有效的操作数。..."

澄清一下,“操作数是数量。您指的是运算符”。
因此,在 a + b 中,ab 都是操作数。

"...代码的当前版本始终拒绝操作数并返回到 while 循环的顶部。..."

您的条件语句不正确。
使用 or 意味着这些条件中的任何一个都需要为 True

请改用 and

if operand != '/' and operand != '*' and operand != '+' and operand != '-':

可能,更直观的方法是使用 notin 操作

if operand not in '/*+-':