试图制作一个计算器,但代码不起作用。怎么修?

时间:2017-09-28 18:55:56

标签: python calculator

这个程序应该是一个计算器。当我运行程序时,它打印操作

我不知道如何解决这个问题。我一直试图制作一个简单的计算器,在终端上运行几天,但似乎没有任何效果。我想我需要重新定义操作var来打印它。我不知道该怎么做。

#The functions of this program #   
def add(num1, num2):
    return (num1 + num2)

def sub(num1,num2):
    return (num1 - num2)

def mul(num1, num2):
    return (num1 * num2)

def div(num1, num2):
    return (num1 / num2)

##The variables of this program ##
num1 = input ("Number 1: ")
num2 = input ("Number 2: ")
operation = input ("Operation: ")

###The if statements of this program ### 
if operation == "add":
      (num1 + num2)

elif operation == "sub":
      (num1 - num2)

elif operation == "mul":
      (num1 * num2)

elif operation == "div":
      (num1 / num2)

####The final code to print the product ####
print operation

3 个答案:

答案 0 :(得分:1)

您未在if声明

中调用您的函数
    if operation == "add":
          print(add(num1,  num2))

    elif operation == "sub":
          print(sub(num1, num2))

    elif operation == "mul":
          print(mul(num1, num2))

    elif operation == "div":
          print(div(num1,  num2))

另请注意,您可以使用dict来抓取该功能并对其进行评估

ops = {'add': add,
       'sub': sub,
       'mul': mul,
       'div': div}

if operation in ops:
    print(ops[operation](num1, num2))
else:
    print('Invalid operator requested')

答案 1 :(得分:0)

您的代码中存在一些问题:

  • 您没有调用您定义的功能。
  • 您没有打印结果,而是打印操作员(甚至没有正确打印)。
  • 您正在对字符串而不是数字应用操作(input返回字符串)。
  • 在Python-2.x中使用raw_input代替input

解决方案:

#The functions of this program #   
def add(num1, num2):
    return (num1 + num2)

def sub(num1,num2):
    return (num1 - num2)

def mul(num1, num2):
    return (num1 * num2)

def div(num1, num2):
    return (num1 / num2)

##The variables of this program ##
num1 = float(raw_input("Number 1: "))   # convert the input to float
num2 = float(raw_input("Number 2: "))   # convert the input to float
operation = raw_input("Operation: ")

# The result variable, it holds an error message, in case the use inputs another operation
result='Unsupported operation'

###The if statements of this program ### 
if operation == "add":
      result = add(num1, num2)

elif operation == "sub":
      result = sub(num1, num2)

elif operation == "mul":
      result = mul(num1, num2)

elif operation == "div":
      result = div(num1, num2)

####The final code to print the product ####
print result

答案 2 :(得分:0)

这就是你需要的.............

python 3和以下代码

确保您使用的是python3而不是python

e.g。 root @Windows-Phone:〜$ python3 anyName.py

#The functions of this program 
def add(num1, num2):
 return (num1 + num2)

def sub(num1,num2):
 return (num1 - num2)

def mul(num1, num2):
 return (num1 * num2)

def div(num1, num2):
 return (num1 / num2)

#The variables of this program
num1 = int(input ("Number 1: "))
num2 = int(input ("Number 2: "))
operation = input ("Operation: ")

#The if statements of this program 
if operation == "add":
  print(add(num1, num2))

if operation == "sub":
  print(sub(num1 ,num2))

if operation == "mul":
  print(mul(num1,num2))

if operation == "div":
  print(div(num1,num2))
相关问题