Global variable can39t be assigned in Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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

Answers

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.

http://docs.python.org/tutorial/controlflow.html#defining-functions 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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/10036747/global-variable-cant-be-assigned-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils