简体   繁体   中英

Global variable Issues with different functions (using Python)

I am trying to create a program that allows me to use my list 'users' in a different function in the program. I have tried parameters, declaring it 'global' (although I've been told it's bad practice to do that) and I've tried researching 'class' and implementing it all to no avail. I have also tried putting users into a text file and then reading from it but it's a nuisance as I need it to be in a list. Here is my code:

def register():
    global username
    username = raw_input("Enter a username: ").lower()
    firstName = raw_input("Enter your first name: ").lower()
    surname = raw_input("Enter your surname: ").lower()
    age = raw_input("Enter your age: ")
    yearGroup = raw_input("Enter your year group: ")
    users =[[firstName, surname, age, yearGroup]] 

def resultsFunction(): 
    score = 5
    results = [[score, username]]
    results.extend(users)

I have tried:

def register():
    global username
    username = raw_input("Enter a username: ").lower()
    firstName = raw_input("Enter your first name: ").lower()
    surname = raw_input("Enter your surname: ").lower()
    age = raw_input("Enter your age: ")
    yearGroup = raw_input("Enter your year group: ")
  global users 
    users =[[firstName, surname, age, yearGroup]] 

It throws up the error:

    results.extend(userDetails)
NameError: global name 'userDetails' is not defined

The strange thing is, after declaring username 'global' (yes, I know I shouldn't, but it worked), I was able to use the username in the function but when I tried it with the other variables, eg firstName so I could try to create the list simply in the 'resultsFunction' it wouldn't work.

The register function does not get run all the time if the user decides not to register but I can't change that as I do not want the user to have to always put in their details.

I am so confused and have tried everything I know. I came here as a last resort so I hope that someone can help and that maybe this question can help others having the same difficulties with the local and global variables scopes.

Write global users inside the function to change values in the users variable. Otherwise you can only read it but not change the value inside it.

lis = []
def a():
    lis = [1,2]
a()
print(lis)

will print []

where as :

lis = []
def a():
    global lis
    lis = [1,2]
a()
print(lis)

will print [1,2]

With reference to your program as per my understanding.. this might help..

username = ''
user = []
def register():
    global username
    global users
    username = input("Enter a username: ").lower()
    firstName = input("Enter your first name: ").lower()
    surname = input("Enter your surname: ").lower()
    age = input("Enter your age: ")
    yearGroup = input("Enter your year group: ")
    users =[[firstName, surname, age, yearGroup]]
def resultsFunction(): 
    score = 5
    results = [[score, username]]
    results.extend(users)
    print(results)
register()
resultsFunction()

will output :-

Enter a username: Foo
Enter your first name: Bar
Enter your surname: Baz
Enter your age: 00
Enter your year group: 00
[[5, 'foo'], ['bar', 'baz', '00', '00']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM