2016-01-10 84 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