2016-01-10 57 views
-1

请不要太意味着我只开始使用Python,并且想要尝试并完成一个可以使用括号和BIDMAS的计算器。我目前正处于开始部分括号的阶段,但是当我运行我的程序时,它不会正确运行我的def函数。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 

任何帮助将不胜感激!

+1

您的代码段过于宽泛,包含了很多你的问题不相关的代码。为了提高您获得答案的机会,您可能希望限制您的示例代码,使其对被问到的问题至少是不可或缺的。为了帮助你,你可能想要熟悉[我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask)和[如何创建一个最小,完整,和可验证示例](http://stackoverflow.com/help/mcve)。 –

+2

你是不是指'total = calculate(user_input)'? –

+0

你没有定义总的def函数工作得很好 –

回答

2

做这种事情的正确方法是定义返回值的函数。注意回报声明结尾:

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) 
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循环来显示结果。因此,无论您将计算(user_input)是否为变量,结果将为。通过在您的每个如果elif陈述,以便每个机构初始化一个变量来解决这个问题答案
  3. 最后记得回答回答并给它一个局部变量名称而真循环。我给了它名字的答案。这与函数计算的答案不同。它现在是一个新的全局变量,分配值答案来自计算函数。

你也有一些重复的代码。你可以写一个函数来查找参数。

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