简单的python计算器将无法运行

时间:2017-11-05 23:24:25

标签: python

好的,我的代码出了什么问题?我按下跑步,它没有给我任何东西。在get_num它说“太多的位置参数”,然后它只是有问题。我不确定我做错了什么 它说“函数中没有值为num 1参数调用”和“函数中没有值为num 2参数调用”“未使用的变量答案”

def main(): #control
  operation()
  get_num(operation)
  if operation == "+":
    answer = addition()
  elif operation == "*":
   answer = multiplication()
  elif operation == "-":
    answer = subtraction()
  else:
    answer = division()

def operation(): #prompts the user to enter a mathematical symbol & returns that symbol
 print("Enter one of the symbols when asked what operation you would like to preform")
operation = input("Would you like to multiply (*), add (+), subtract (-), or divide(/)? ")
return operation

def get_num(): #prompts the user to enter a single integers & returns that value
  print("Enter two numbers you would like to calculate")
  num1 = float(input("What is the first number you want to calculate? "))
  num2 = float(input("What is the second number you want to calculate? "))
  return num1
  return num2

 def addition(num1, num2):  #performs addition of the 2 numbers & returns the sum
   addition = num1 + num2
   return addition

def subtraction(num1, num2):  #performs subtraction of the second number from the first & returns the difference
  subtraction = num1 - num2
  return subtraction

def multiplication(num1, num2):  #performs multiplication of the 2 numbers & returns the product
 multiplication = num1 * num2
 return multiplication

def division(num1, num2): #if the second number is not zero, performs division of the first number (numerator) by the second number (denominator) and returns the quotient; if the second number is zero, doe not perform the division, but displays a message and returns a zero
  division = num1 / num2
  return division
   if num2 == "0":
    print("Error, cannot divide by 0")

 def show_answer(answer):  #displays the answer from the operation
 if operation == '*':
  print("The answer for your problem is", multiplication)
  elif operation == '-':
   print("The answer for your problem is", subtraction)
  elif operation == '+':
   print("The answer for your problem is", addition)
   elif operation == '/':
    print("The answer for your problem is", division)
   else:
   print("error")

4 个答案:

答案 0 :(得分:0)

嘿,你的程序是用简洁的方式编写的,但你需要将变量传递给函数.....你不能像这样定义全局变量

答案 1 :(得分:0)

以下是我在您的问题/代码中看到的一些问题:

  1. 您的代码缩进很差。我不能真正尝试你的代码,因为你有这些奇怪的注释。

  2. 您的函数调用必须具有与定义时相同的参数量func(x, y)必须始终使用2个变量声明func('Hello', 'World')

  3. return会返回一个数据,但不会自行保存。如果您对该数据有用,则必须将该值赋给变量。

  4. 您不太了解变量的本地和全球范围

  5. 无论如何要避免询问"为什么我的代码不能正常工作"问题

    试试这段代码:

    def main(): #control
        operation = operate()
        num1, num2 = get_num()
        if operation == "+":
            answer = addition(num1, num2)
        elif operation == "*":
            answer = multiplication(num1, num2)
        elif operation == "-":
            answer = subtraction(num1, num2)
        elif operation == "/":
            answer = division(num1, num2)
        else:
            print ("Invalid Operation")
        show_answer(answer)
    
    def operate(): #prompts the user to enter a mathematical symbol & returns that symbol
        print("Enter one of the symbols when asked what operation you would like to preform")
        operation = input("Would you like to multiply (*), add (+), subtract (-), or divide(/)? ")
        return operation
    
    def get_num(): #prompts the user to enter a single integers & returns that value
        print("Enter two numbers you would like to calculate")
        num1 = float(input("What is the first number you want to calculate? "))
        num2 = float(input("What is the second number you want to calculate? "))
        return num1, num2
    
    def addition(num1, num2):  #performs addition of the 2 numbers & returns the sum
        addition = num1 + num2
        return addition
    
    def subtraction(num1, num2):  #performs subtraction of the second number from the first & returns the difference
        subtraction = num1 - num2
        return subtraction
    
    def multiplication(num1, num2):  #performs multiplication of the 2 numbers & returns the product
        multiplication = num1 * num2
        return multiplication
    
    def division(num1, num2): #if the second number is not zero, performs division of the first number (numerator) by the second number (denominator) and returns the quotient; if the second number is zero, doe not perform the division, but displays a message and returns a zero
        try:
            division = num1 / num2
            return division
        except ZeroDivisionError:
            print ("Error, cannot be divided by 0")
    
    def show_answer(answer):
        print ("The answer for your problem is", answer)
    
    main()
    

    您也可以更改您的divison的else-if语句,以检查num2的值是否为0:

    elif operation == '/':
        if num2 == 0:
            return ('Error, cannot be divided by 0')
        else:
            answer = division(num1, num2)
    

答案 2 :(得分:0)

首先你写了get_num(operation)但是你的函数有0个参数:def get_num():

答案 3 :(得分:0)

需要改进的地方:

  • Python是一种脚本语言,所以你必须调用底部的main()函数来启动它

  • 使用raw_input("message")而不是input("mesage")并使用operator = str(raw_input("message"))

  • 自行转换
  • 您正在呼叫get_num(operation),但operation()是一个函数,而不是值。也许您应该将get_num(operation)的返回值存储到operator = get_num(operation)

  • 之类的变量中

以下是我对您的代码进行一些改进的方法:

def main(): #control
    operator = operation()
    nums = get_num()

    if operator == '+':
        answer = addition(nums[0], nums[1])

    elif operator == '*':
        answer = multiplication(nums[0], nums[1])

    elif operator == '-':
        answer = subtraction(nums[0], nums[1])

    elif operator == '/':
        answer = division(nums[0], nums[1])

    else:
        raise ValueError("'" + operator + "' Operand not recognized")

    # Display result
    answer = str(answer)
    print(str(nums[0]) + operator + str(nums[1]) + " = " + answer)

def operation(): #prompts the user to enter a mathematical symbol & returns that symbol
    print("Enter one of the symbols when asked what operation you would like to preform")

    operator = raw_input("Would you like to multiply (*), add (+), subtract (-), or divide(/)? ")

    # Convert it to a string
    return str(operator)

# NOTE - no need to pass the operator
def get_num(): #prompts the user to enter a single integers & returns that value
    print("Enter two numbers you would like to calculate")
    num1 = float(raw_input("What is the first number you want to calculate? "))
    num2 = float(raw_input("What is the second number you want to calculate? "))

    # NOTE - Return as an array rather than 2 return statements
    return [num1, num2]

def addition(num1, num2):  #performs addition of the 2 numbers & returns the sum
    addition = num1 + num2
    return addition

def subtraction(num1, num2):  #performs subtraction of the second number from the first & returns the difference
    subtraction = num1 - num2
    return subtraction

def multiplication(num1, num2):  #performs multiplication of the 2 numbers & returns the product
    multiplication = num1 * num2
    return multiplication

def division(num1, num2): #if the second number is not zero, performs division of the first number (numerator) by the second number (denominator) and returns the quotient; if the second number is zero, doe not perform the division, but displays a message and returns a zero

    # NOTE - Throw an error if denominator is 0
    if num2 == 0:
        raise ValueError("Divistion by zero")

    division = num1 / num2
    return division


# Remember to call main() to kick off the program
main()