We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,473 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Update a function global varaible

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

4
Contributors
5
Replies
23 Hours
Discussion Span
3 Months Ago
Last Updated
20
Views
Question
Answered
longtomjr
Newbie Poster
23 posts since Feb 2013
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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?

mat1998x
Junior Poster
185 posts since Dec 2012
Reputation Points: 0
Solved Threads: 11
Skill Endorsements: 0

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
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9

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

longtomjr
Newbie Poster
23 posts since Feb 2013
Reputation Points: 0
Solved Threads: 2
Skill Endorsements: 0

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
snippsat
Posting Shark
957 posts since Aug 2008
Reputation Points: 482
Solved Threads: 344
Skill Endorsements: 8

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()
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9
Question Answered as of 3 Months Ago by woooee, snippsat and mat1998x

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0699 seconds using 2.7MB