Working with functions (basic Python)

I'm a beginner in programming ( python ) and doing some recent homework, but I'm really stuck with this simple function implementation. Basically, I have to ask for user input done in main (), then define another function to compute the computation with variables assigned for user input. When run in IDLE it will output 0. Any help or hints would be extremely helpful, thanks. Here is the code I have so far:

#global variables
firstClass = 0.0
businessClass = 0.0
economyClass = 0.0
soldFirstClass = 0.0
soldBusinessClass = 0.0
soldEconomyClass = 0.0
totalFC = 0.0
totalBC = 0.0
totalEC = 0.0

######################################
###########MAIN FUNCTION#############
######################################

def main():

    print('***WELCOME***\n')
    print('***Please ENTER the Airlines ticket prices***\n')

    firstClass = float(input ('Please ENTER a ticket price for First Class: '))
    businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
    economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))

    print('\nThank You\n')

    soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
    soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
    soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))

    calcClass()

def calcClass():

    global firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass

    totalFC = firstClass*soldFirstClass
    print('Total money earned for First Class = %0.0f ') % totalFC
    totalBC = businessClass*soldBusinessClass
    print('Total money earned for Business Class = %0.0f ') % totalBC
    totalEC = economyClass*soldEconomyClass
    print('Total money earned for Economy Class = %0.0f ') % totalEC

main()

      

+3


source to share


4 answers


Yours main()

is assigning local variables. Copy the operator global …

from calcClass()

to main()

.

Having said that, using globals to pass information around is almost always a bad idea. You must pass them as parameters:



# No globals; just go straight into main
def main():
    print('***WELCOME***\n')
    print('***Please ENTER the Airlines ticket prices***\n')

    firstClass = float(input ('Please ENTER a ticket price for First Class: '))
    calcClass(firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass)

def calcClass(firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass):
    totalFC = firstClass*soldFirstClass
    print('Total money earned for First Class = %0.0f ') % totalFC

      

+2


source


The first problem in your code is what you are using global

backwards (this is actually one of the most confusing things in python).

Use global to declare which variables from the global scope you want to write (you use it to define the variables you want to read). As long as you don't write a variable with the same name as a global variable, you can read it from that global without using the global declaration.



Accordingly, the smallest change you can make to your code to make it work is to add the correct global declarations to both of your functions, identifying the global variables that you will assign.

However, the right thing to do is eliminate any globals you want to write. Instead, add parameters to your functions and pass values ​​between them using parameters.

+2


source


Don't use global variables. Instead, let's calcClass

take arguments, like this:

totalFC = 0.0
totalBC = 0.0
totalEC = 0.0

def main():
    print('***WELCOME***\n')
    print('***Please ENTER the Airlines ticket prices***\n')

    firstClass = float(input ('Please ENTER a ticket price for First Class: '))
    businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
    economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))

    print('\nThank You\n')

    soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
    soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
    soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))

    calcClass(firstClass, businessClass, economyClass, soldFirstClass, soldBusinessClass, soldEconomyClass)

def calcClass(firstClass, businessClass, economyClass, soldFirstClass, soldBusinessClass, soldEconomyClass):
    totalFC = firstClass*soldFirstClass
    print('Total money earned for First Class = %0.0f ') % totalFC
    totalBC = businessClass*soldBusinessClass
    print('Total money earned for Business Class = %0.0f ') % totalBC
    totalEC = economyClass*soldEconomyClass
    print('Total money earned for Economy Class = %0.0f ') % totalEC

if __name__ == '__main__':
    main()

      

0


source


I think you just made a small mistake.

In this module, "Main Function" is main()

also a function similar to "calcClass ()".

So, when you want to use these globals in main()

, you must add this operator to the main function as you did for the functioncalcClass()

 global firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass

      

So the main function looks like this:

def main():

    # NOTE THIS LINE.
    global firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass

    print('***WELCOME***\n')
    print('***Please ENTER the Airlines ticket prices***\n')

    firstClass = float(input ('Please ENTER a ticket price for First Class: '))
    businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
    economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))

    print('\nThank You\n')

    soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
    soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
    soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))

    calcClass()

      

Or else, you could do it instead by declaring the function arguments calcClass()

and passing those variables as parameters.

this is how the code will look like:

def main():

    print('***WELCOME***\n')
    print('***Please ENTER the Airlines ticket prices***\n')

    firstClass = float(input ('Please ENTER a ticket price for First Class: '))
    businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
    economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))

    print('\nThank You\n')

    soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
    soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
    soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))

    # NOTE THIS LINE
    calcClass(firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass)

# NOTE THIS LINE
def (calcClass firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass):

    totalFC = firstClass*soldFirstClass
    print('Total money earned for First Class = %f ') % totalFC
    totalBC = businessClass*soldBusinessClass
    print('Total money earned for Business Class = %f ') % totalBC
    totalEC = economyClass*soldEconomyClass
    print('Total money earned for Economy Class = %f ') % totalEC


main()

      

0


source







All Articles