测试是否输入了值

Testing if a value was entered

我写了一个计算器,我希望能够输入两个或三个数字。 目前这是我所拥有的:

def main():
    action2=0
    user_input=input("Enter a num1 act1 num2 act2 num3 (with a space between them): ")      #Gets the values
    var1, action1, var2, action2, var3=user_input.split()   #assigns the values into the variables
    if(action2==0):
        calc2(float(var1), action1, float(var2))
    else:
        calc3(float(var1), action1, float(var2), action2, float(var3))

如何让它发挥作用? 它给我一个错误,我需要 5 个变量用于 split() 操作,这是有道理的。我的问题是我能做什么(什么方式)我能做我想做的事吗?

完整代码:

def calculate(num1, num2, act):
    if(act=='+'):
        total=num1+num2
    elif(act=='-'):
        total=num1-num2
    elif(act=='*'):
        total=num1*num2
    elif(act=='/'):
        total=num1/num2
    else:
        print("input not recognized")
    return total

def calc2(var1, action1, var2):
    if(action1=='/' and var2==0):      #checks for division by 0
        print("YOU CAN'T DIVIDE BY ZERO!!!")
    else:
        print(calculate(float(var1), float(var2), action))     #calls the 'calculating' function, recives and prints the total of act

def main():
    action2=0        #testing if anything was entered as action2
    user_input=input("Enter a num1 act1 num2 act2 num3 (with a space between them): ")      #Gets the values
    var1, action1, var2, action2, var3=user_input.split()   #assigns the values into the variables
    if(action2==0):   #two num calc
        calc2(float(var1), action1, float(var2))
    else:    #three num calc
        calc3(float(var1), action1, float(var2), action2, float(var3))

def calc3(var1, action1, var2, action2, var3):
    if(action1=='/' and var2==0 or action2=='/' and var3==0):      #checks for division by 0
        print("YOU CAN'T DIVIDE BY ZERO!!!")
    elif((action2=='*' or action2=='/') and (action1=='+' or action2=='-')):    #checks if act2 should be done before act1 (order of operation) total=calculate(float(var2), float(var3), action2)     #calls the 'calculating' function, recives the total of act2
        total=calculate(float(var2), float(var3), action2)
        print(calculate(float(var1), float(total), action1))     #calls the 'calculating' function, assigns the total of act2 as num2, recives and prints the total of act1
    else:                                                             #act1 is done before act2 (order of operation)
        total=calculate(float(var1), float(var2), action1)         #calls the 'calculating' function, recives the total of act1
        print(calculate(float(total), float(var3), action2))     #calls the 'calculating' function, assigns the total of act1 as num1, recives and prints the total of act2



main()           #starts program

如果要解包五个值,则必须实际上有 5 个值。您不能将 3 个值解包为 5 个变量。更改您的代码以检查在解包之前收到了多少输入。

user_input=input("Enter a an expression (with a space between tokens): ")
inputs_amt = len(user_input.split())
if inputs_amt == 5:
  var1, action1, var2, action2, var3 = user_input.split() 
elif inputs_amt == 3:
  var1, action1, var2 = user_input.split()
else:
  print "Invalid input, try again"

您还必须重写计算代码以解决此差异。

编辑:

user_input.split() 生成一个字符串列表。如果你像这样解压这个列表:

var1, action1, var2, action2, var3 = user_input.split()

那么列表中肯定有五个元素(数字或运算符),因为它被解包为五个变量。如果有任何其他数量的元素,它会抛出一个错误。

因此,在解包之前,您必须检查 split() 会产生多少元素。您也可以跳过解包并遍历列表,保持 运行 总数。

total = 0
op = "+"
for foo in user_input.split():
  if foo.isdigit():
    if op == "+":
      total += int(foo)
    if op == "-":
      total -= int(foo)
    if op == "*":
      total *= int(foo)
    if op == "/":
      total /= int(foo)
  else:
    op = foo

print(total)

EXTRA:您评估表达式的策略未能考虑运算符优先级,并且需要额外的代码来处理更长(或更短)的表达式。 See here 更复杂但更强大的算术表达式解析器。