I am having trouble getting a variable from one defined function to another. For example, I want user to select a country from a menu and then that country to be saved in a variable for the first function. After that, user has to select a package they want from the second menu and then tell the program how many people ages 12+, 2+ and 2- are going.
Once it is done, I want all the saved information to go to a table, for example if a user had selected Spain and full board package and there are 2 people aged 12+ and 1 people aged 2+i want the program to go to Spain table and add the prices. the prices are different for all ages.
below is the code I got so far, I was wondering if anyone could help with it.
def result():
global spian
global spianf
total=spian*n+spianf
print total
def total():
global n
global i
result()
def age ():
n=input("please select number of people age 12+")
m=input("please select number of people age 2+ ")
b=input("please select number of people age 2-")
total()
result()
def pack():
print "1.Full Boaard"
print "2.Half board"
print "3.Beds only"
print "4.Main menu"
n=raw_input("please select you package ")
if n=="1":
age()
return n;
if n=="2":
age()
if n=="3":
age()
if n=="4":
country()
def country ():
print "1.Spain"
print "2.Portugal"
print "3.Italy"
print "4.Turkey"
print "5.Exit"
i=raw_input("Please enter your choice of country ")
if i=="1":
pack()
spain=1
if i=="2":
pack()
if i=="3":
pack()
if i=="4":
pack()
if i=="5":
exit
country()
spian=150
spianf=50
If you are just starting out learning python, I would highly recommend not developing a habit like this of using globals for the variables in all of your functions.
Take a moment to review this page from the python docs: http://docs.python.org/tutorial/controlflow.html#defining-functions
For instance, while you could technically fix your problem in this function:
def age ():
global n,m,b
n=input("please select number of people age 12+")
m=input("please select number of people age 2+ ")
b=input("please select number of people age 2-")
... It would be much more useful for your function to return something
def age ():
n=input("please select number of people age 12+")
m=input("please select number of people age 2+ ")
b=input("please select number of people age 2-")
return n, m, b
# and call it like
n, m, b = age()
If you have a function that wants to modify your spian
, you might do something like this:
def newSpian(spianVal):
return spianVal * 100
# and call it like
newSpianValue = newSpian(spian)
# or overwrite the old one
spian = newSpian(spian)
This will make your functions far more reuseable, and also easier to understand. When you make use of globals for everything like this, its hard to know where a variable even comes from in the logic. You have to look through all of your other functions to figure out what might have changed it along the way, or even created its current value.
I would also recommend you use more useful variable names than a,b,c,d
You're using the global keyword wrong.
You should use it inside the functions which you want to look globally. For example:
i = 5
def f(x):
global i = x+1
f(2)
print i
will print 3
What you're doing, in comparison, is more like
global i = 5
def f(x):
i = x+1
f(2)
print i
which prints 5
Also, your formatting when you posted is kinda screwey, so it makes the code hard to read. But I think this is your issue.
global i
to state that its global, and then continue to modify i
on subsequent lines - jdi 2012-04-05 22:39