Reputation: 71
I ask for user input executed in main()
then define another function to work out the calculation with variables assigned to the user input. In IDLE it outputs 0:
#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()
Upvotes: 3
Views: 339
Reputation: 4828
I think you just made a small mistake.
In this "Main Function" module, main()
is also a function just like 'calcClass()`.
So, when you want to use those global variables in main()
, you have to add this statement to the main function just like you did for calcClass()
function
global firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass
So, the main function looks like:
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, instead of doing this, you could have done it by declaring arguments to the function calcClass()
and passing those variables as parameters.
this is how the code would 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()
Upvotes: 0
Reputation: 49816
The first problem in your code is that you have the use of global
backwards (this is actually one of the more confusing things in python).
Use global to declare which variables from the global scope you want to write to (you are using it to identify the variables you want to read from). As long as you don't write to a variable with the same name as a global variable, you can read from that global without having to use the global declaration.
Accordingly, the least change you can make to your code to make it work is to add the right global declarations to both of your functions, identifying the global variables you will be assigning to.
However, the right thing to do is to eliminate any global variables that you want to write to. Instead, add parameters to your functions, and pass the values between them using parameters.
Upvotes: 2
Reputation: 287775
Don't use global variables. Instead, let 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()
Upvotes: 0
Reputation: 185842
Your main()
is assigning to local variables. Copy the global …
statement from calcClass()
to main()
.
Having said that, using global variables to pass information around is almost always a bad idea. You should 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
⋮
Upvotes: 2