测试是否输入了值

时间:2016-12-22 16:07:00

标签: python python-3.x input user-input calculator

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

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

1 个答案:

答案 0 :(得分:0)

如果要解压缩五个值,则必须实际具有 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用于更复杂但更强大的算术表达式解析器。