【问题标题】:Trying to make a calculator, but code isn't working. How to fix?试图制作一个计算器,但代码不起作用。怎么修?
【发布时间】:2017-09-28 18:55:56
【问题描述】:

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

我不确定如何解决这个问题。我一直在尝试制作一个在终端中运行几天的简单计算器,但似乎没有任何效果。我想我需要重新定义操作 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

【问题讨论】:

    标签: python calculator


    【解决方案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')
    

    【讨论】:

      【解决方案2】:

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

      • 您没有调用您定义的函数。
      • 您不是在打印结果,而是在打印运算符(甚至不正确)。
      • 您正在对字符串而不是数字应用操作(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
      

      【讨论】:

      • 当我使用这段代码时,我实际上必须在控制台中输入“add”和引号,否则它不起作用。有没有办法解决这个问题?
      • @Logster 您尝试过还是只是在观察,使用此代码您只需在控制台中输入add 而不是"add",试试看。
      • 我试过了,只有当我使用“add”时它才有效。使用 add 显示错误消息。
      • @Logster 你使用的是 Python-2.x,你必须使用 raw_input 而不是 input
      • 谢谢你,你说得对,我不知道为什么我用python 2,我安装了python 3,它说它成功了。
      【解决方案3】:

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

      python 3 及以下代码

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

      例如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))
      

      【讨论】:

        猜你喜欢
        • 2022-11-22
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        • 2022-12-03
        • 2018-01-05
        • 1970-01-01
        • 2021-09-11
        • 1970-01-01
        相关资源
        最近更新 更多