def功能无法正常运行

时间:2016-01-10 18:42:13

标签: python python-3.x calculator function

请不要太吝啬我只是开始使用Python并想尝试完成一个可以使用括号和BIDMAS的计算器。我目前处于开始关于括号的部分的阶段,但是当我运行我的程序时,它将无法正常运行我的def函数。

import time
import os
import random
import sys

print("=======================")
print("Use ^ for the power of")
print("Use + for adding")
print("Use - for subtracting")
print("Use * for multiplying")
print("Use / for dividing")
print("Use % for a percentage")
print("DON'T USE SPACES!")
print("=======================\n\n")

uilist = ""
uilength = ""
user_input = ""

def to_the_power_of(a):
    postion_n1 = a.index("^")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1**n2
    sign = "^"

def subtracting(a):
    postion_n1 = a.index("-")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1-n2
    sign = "-"

def adding(a):
    postion_n1 = a.index("+")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1+n2
    sign = "+"

def multiplying(a):
    postion_n1 = a.index("*")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1*n2
    sign = "x"

def dividing(a):
    postion_n1 = a.index("/")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1/n2
    sign = "/"

def percentage(a):
    postion_n1 = a.index("%")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = (n1*n2)/100
    sign = "%"

def calculate(ab):
    uilength = len(ab)
    uilist = list(ab)
    if "^" in uilist:
        to_the_power_of(answer_1)

    elif "+" in uilist:
        adding(answer_1)

    elif "-" in uilist:
        subtracting(answer_1)

    elif "*" in uilist:
        multiplying(answer_1)

    elif "/" in uilist:
        dividing(answer_1)

    elif "%" in uilist:
        percentage(answer_1)

    else:
        print("Please eneter a valid calculation!")

while True:
    user_input = input("Write your calculation: ")


    answer_1 = user_input
    calculate(user_input)

    print(user_input,"=",total)

    exit_cal = input("Would you like to exit or make a now calculation?").lower()
    exit_list = ["exit","quit","leave","404"]
    if exit_cal in exit_list:
        sys.exit()

这是我得到的错误

=======================
Use ^ for the power of
Use + for adding
Use - for subtracting
Use * for multiplying
Use / for dividing
Use % for a percentage
DON'T USE SPACES!
=======================


Write your calculation: 12*4
Traceback (most recent call last):
  File "C:\Users\Max\Desktop\Python Scripts\Calculator.py", line 99, in <module>
    print(user_input,"=",total)
NameError: name 'total' is not defined

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

执行此类操作的正确方法是定义返回值的函数。请注意最后的 return 语句:

def subtracting(a):
  postion_n1 = a.index("-")
  postion_n2 = postion_n1 + 1
  n1 = int(a[:postion_n1])
  n2 = int(a[postion_n2:])
  return n1-n2

然后,您可以这样打印结果:

total = subtracting(ab)
print(user_input,"=",total)

答案 1 :(得分:0)

我修改了你的代码并在我做出更改的地方添加了评论。如果进行了相同的更改,我只需添加 ...

import time
import os
import random
import sys

print("=======================")
print("Use ^ for the power of")
print("Use + for adding")
print("Use - for subtracting")
print("Use * for multiplying")
print("Use / for dividing")
print("Use % for a percentage")
print("DON'T USE SPACES!")
print("=======================\n\n")

uilist = ""
uilength = ""
user_input = ""

def to_the_power_of(a):
    postion_n1 = a.index("^")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1**n2
    sign = "^"
    return total #Remember if you want your function to produce a result other than None, you must use keyword return because variables inside functions exist only in functions until returned and assigned a variable in another scope.

def subtracting(a):
    postion_n1 = a.index("-")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1-n2
    sign = "-"
    return total #...

def adding(a):
    postion_n1 = a.index("+")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1+n2
    sign = "+"
    return total #...

def multiplying(a):
    postion_n1 = a.index("*")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1*n2
    sign = "x"
    return total #...

def dividing(a):
    postion_n1 = a.index("/")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = n1/n2
    sign = "/"
    return total #...

def percentage(a):
    postion_n1 = a.index("%")
    postion_n2 = postion_n1 + 1
    n1 = int(a[:postion_n1])
    n2 = int(a[postion_n2:])
    total = (n1*n2)/100
    sign = "%"
    return total #...

def calculate(ab):
    uilength = len(ab)
    uilist = list(ab)
    if "^" in uilist:
        answer = to_the_power_of(question) #You did the same mistake here. But this is part 2 of the mistake. You need a variable to return from calculate.

    elif "+" in uilist:
        answer = adding(question) #...

    elif "-" in uilist:
        answer = subtracting(question) #...

    elif "*" in uilist:
        answer = multiplying(question) #...

    elif "/" in uilist:
        answer = dividing(question) #...

    elif "%" in uilist:
        answer = percentage(question) #...

    else:
        print("Please eneter a valid calculation!")

    return answer #This will allow calculate to return the result

while True:
    user_input = input("Write your calculation: ")

    question = user_input
    answer = calculate(user_input) #Assign a variable for the result being returned from calculate.

    print(user_input,"=",answer)

    exit_cal = input("Would you like to exit or make a now calculation?").lower()
    exit_list = ["exit","quit","leave","404"]
    if exit_cal in exit_list:
        sys.exit()

干得好,您可能想要探索的概念。这将使您的程序更加健壮。

以下是您的问题:

  1. 您的数学函数(加,减,乘等)未返回任何值。结果,他们实际上在进行计算,但没有返回结果。您已将结果保存为值总计,您应使用返回总计返回该值。所有涉及计算和总计的函数都存在此问题。
  2. 您的计算函数未将结果返回到您的while循环以显示为结果。因此,无论您是否将 calculate(user_input)分配给变量,结果都是。通过在每个 if elif 语句中放置变量赋值来解决此问题,以便每个正文初始化变量 answer
  3. 最后请记住返回答案,并在 while True 循环中为其指定一个本地变量名称。我给它起了名字答案。这与函数计算中的答案不同。它现在是一个新的全局变量,从计算函数中分配答案的值。
  4. 你也有一些重复的代码。你可以编写一个函数来查找参数。

    def findArguments(question,operator):
        postion_n1 = question.index(operator)
        postion_n2 = postion_n1 + 1
        n1 = int(question[:postion_n1])
        n2 = int(question[postion_n2:])
        return (n1,n2) #This is a tuple of the 2 integers.
    

    使用示例:

    def dividing(a):
        arguments = findArguments(a,"/")
        total = arguments[0]/arguments[1]
        return total