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

问题描述 投票:0回答:1

我正在编写代码来用 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 validation while-loop boolean user-input
1个回答
0
投票

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

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

“...当前版本的代码总是拒绝操作数并返回到 while 循环的顶部。...”

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

请改用

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

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

if operand not in '/*+-':
© www.soinside.com 2019 - 2024. All rights reserved.