简体   繁体   中英

Python - I'm not sure why this function I'm defining isn't working. Very fresh to programming

I'm trying to write a small program that calculates the discount of a coupon and then return the total with a 6% sales tax applied. I've done some searching, but I think I'm having trouble understanding the placement of the syntax. If there's a more direct answer already posted, I'll take the link. Thanks in advance.

#Apply discount amount to item price and return with 6% sales tax

#Function to perform discount calculation
def calcDisc():
    newTotal = iPrice * (iDisc / 100)
    print('The new total is: ', newTotal)
    return newTotal

#Function to show total with 6% sales tax
def calcDiscPlusTax():
    taxTotal = newTotal * 0.6
    print('The total with tax is: ', taxTotal)
    
#Variable for item price
iPrice = float(input('Please enter the item price: '))

#Variable for item with discount applied
iDisc = float(input('Please enter the discount amount: '))

calcDisc()

calcDiscPlusTax()

As a general rule, values that you use inside the function should be parameters to the function, just like values that you need to return to the caller should be return ed by the function. For example:

#Apply discount amount to item price and return with 6% sales tax

#Function to perform discount calculation
def calcDisc(iPrice, iDisc):
    newTotal = iPrice * (iDisc / 100)
    print('The new total is: ', newTotal)
    return newTotal

#Function to show total with 6% sales tax
def calcDiscPlusTax(newTotal):
    taxTotal = newTotal * 0.6
    print('The total with tax is: ', taxTotal)

At the time you call the function, you pass it arguments that tell it what values to use for its defined parameters:

#Variable for item price
iPrice = float(input('Please enter the item price: '))

#Variable for item with discount applied
iDisc = float(input('Please enter the discount amount: '))

newTotal = calcDisc(iPrice, iDisc)

calcDiscPlusTax(newTotal)

The arguments and parameters don't need to have the same names; the parameter is used inside the function definition, and the argument is a value that exists outside of it (in the scope where the function is called). So you can also call your functions with different variable names:

#Variable for item price
price = float(input('Please enter the item price: '))

#Variable for item with discount applied
discount = float(input('Please enter the discount amount: '))

subtotal = calcDisc(price, discount)

calcDiscPlusTax(subtotal)

or even no variable names at all:

calcDiscPlusTax(
    calcDisc(
        float(input('Please enter the item price: ')),
        float(input('Please enter the discount amount: '))
    )
)

OK, so first things first, you're calling your functions, but you're not passing values to those functions, so when you calculate something in your functions it will be lost after their execution ends.

calcDist uses iPrice and iDisc variables, however those variables does not exist in that space. Solution to this could be:

def calcDisc(price, disc):
    total = price * (disc / 100)
    print('The new total is: ', total)
    return total

In the calcDiscPlusTax() you're using newTotal but that function does not know what's that:

def calcDiscPlusTax(total):
    taxTotal = total * 0.6
    print('The total with tax is: ', taxTotal)

So while calling those functions you have to pass parameters to them, to let them know about values you provided:

total = calcDisc(iPrice, iDisc)
calcDiscPlusTax(total)

Also, the function arguments don't have to be the same name as the parameters, please take a look at how I've called calcDisc (with iPrice, iDisc), and how I retrieve arguments in the function (just price and disc, but could be num1 and num2 as well)

You can find more by searching for python variable scope

As a side note, please rename functions and variables according to Python guidelines, which will be new_total, calc_disc_plus_tax, calc_disc, and so on.

If you define a variable outside of a function, it won't know what that is within a function unless you provide it as a variable

A = 5
def SomeFunction():
    print(A)

Will actually throw an error because the SomeFunction doesn't actually have access to information outside of itself. Think about your functions as being a separate room, if you need something from the living room - you need to go get it and bring it in. So in your case, you've defined iPrice and iDisc, but your functions don't have access to that data. There's a couple ways to fix it. The first being providing them as variables:

#Function to perform discount calculation
def calcDisc(iprice,idisc):
    newTotal = iPrice * (iDisc / 100)
    print('The new total is: ', newTotal)
    return newTotal

#Function to show total with 6% sales tax
def calcDiscPlusTax(newtotal):
    taxTotal = newTotal * 0.6
    print('The total with tax is: ', taxTotal)

#Variable for item price
iPrice = float(input('Please enter the item price: '))

#Variable for item with discount applied
iDisc = float(input('Please enter the discount amount: '))

NewTotal = calcDisc(iPrice,iDisc)

print( calcDiscPlusTax(NewTotal) )

So here what we've done is add in variables to our functions and we're passing those inputs into them, recording the output to NewTotal, and then passing that value as a variable to the calcDiscPlusTax.

However, there is another option --- check "globally" for the variables. For this approach, what we do is tell the function to essentially "go look outside for a variable called xyz and remember it", which would look like:

#Function to perform discount calculation
def calcDisc():
    global iPrice,iDisc
    newTotal = iPrice * (iDisc / 100)
    print('The new total is: ', newTotal)
    return newTotal  #<--- NOTE*

#Function to show total with 6% sales tax
def calcDiscPlusTax():
    global newTotal
    taxTotal = newTotal * 0.6
    print('The total with tax is: ', taxTotal)

NOTE* However, keep in mind that when you 'return' something from a function, it doesn't create a variable -- it just sends that info out. So, make sure when you 'return newTotal' that you have a variable to record that output. I added that in the other example but that would look something like

#Function to perform discount calculation
def calcDisc():
    global iPrice,iDisc
    newTotal = iPrice * (iDisc / 100)
    print('The new total is: ', newTotal)
    return newTotal

someVariableName = calcDisc()

And now you can use your output data anywhere: :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM