I have been playing around a bit and I want to know if you can update a global varaible...

rowA = []

def printSeat():
    global rowA

    print "A",
    rowA = ['*', '*', '*', '*', '*', '*', '*', '*']
    print rowA

    print "B",
    rowB = ['*', '*', '*', '*', '*', '*', '*', '*']
    print rowB

    print "C",
    rowC = ['*', '*', '*', '*', '*', '*', '*', '*']
    print rowC

    print "D",
    rowD = ['*', '*', '*', '*', '*', '*', '*', '*']
    print rowD

    print "E",
    rowE = ['*', '*', '*', '*', '*', '*', '*', '*']
    print rowE

    print "F",
    rowF = ['*', '*', '*', '*', '*', '*', '*', '*']
    print rowF

printSeat()

print
print
print


input1 = raw_input('$')


if input1[:1] == 'A' or 'a':
    if input1[3:5] == '-o':
        seat = int(input1[1:2])
        print rowA
        print seat
        rowA[0:int(seat)] = ['X']
        print rowA


        printSeat()

I basicly want 'rowA' the same as 'rowA' inside the function, so when I run "printSeat()" i wnat it to show row A changed

If it is unclear what i am tring to say I will try to explain in another way

Recommended Answers

All 5 Replies

Im not completely sure what your saying but any of these might help:
you can update global variables whenever you want just re-use them:

def rowA():
    rowA = "Apples"

def blah():
    global rowA
    print(rowA)
    rowA = "bannana"
    print(rowA)

rowA()
blah()

Which would give back Apple the bannana

Sorry if I got the wrong idea but this is what I think your saying

or

you have said you want rowA the same as inside the function? you have made rowA at the top? But the you are re assigning it in printSeat()?

What is your program trying to do exactly?

Lists are mutable, which means they can be used without global. The problem is possibly in the code you use to update the list; it truncates the list also. Run the code example below. You are asking questions that are in most tutorials so you should start with a tutorial instead of asking us to answer questions that every beginner should know. A place to start = tutorial on lists Click Here Python Wiki list of tutorials Click Here

rowA = ['*', '*', '*', '*', '*', '*', '*', '*']
seat=5
rowA[0:int(seat)] = ['X']  ## copies up to "seat" only
print "update 1", rowA 
#
# instead
rowA[0]="Y"
rowA[2]="X"
print "update 2", rowA
#
for ctr in range(0, 3):
    rowA[ctr] = "Z"
print "update 3", rowA

What I want to do is when I run 'printSeat()' the printSeat() rowA must be the same as the rowA defined above... Is there any way to posibly do this or must I try a different fall in point for my program... for example

bar = "global Variable"

def foo():
    global bar
    bar = "Just a string"
    print bar


foo()
#the above will print "Just a string"


bar = "Now we change it outside"

print bar
#the above will print "Now we change it outside"

foo()
#the above will print "Just a string"  - i want it to print "Now we change it outside"

Is this at all posible without taking the variable out of "foo()"

@woooee:
yes I may be nosy ect. but I do learn more doing the beginner and other projects than following a tutorial. I am nearly finished with "How tio think like a Computer sienstist" and about to do the next book, please bear with me. I treid google but I couldnt find anything...

Kind Regards
Longtomjr

You should almost never use global varible in function,it's ugly.
Function should take argument an return values out.
Some code you can look at.

#Ver_1
def foo():
    bar = "Just a string"
    return bar

print foo() #Just a string

#Ver_2
def foo(global_bar):
    bar = "Just a string and from ouside {}".format(global_bar)
    return bar

global_bar = "global Variable"
print foo(global_bar) #Just a string and from ouside global Variable

#Ver_3
def foo(global_bar="global Variable"):
    '''Here i use default argument'''
    bar = "Just a string and from default arg {}".format(global_bar)
    return bar

print foo() #Just a string and from default arg global Variable

Once again, lists are mutable (passed by reference) so global is unnecessary. Another example :

def print_row():
   print rowA

rowA = ['*', '*', '*', '*', '*', '*', '*', '*']
print_row()

for ctr in [1, 3, 5]:
   rowA[ctr] = "X"
print_row()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.