Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable gets lost
#1
why doesn't this work??
admin = ['Jordy', 'Julian']
logged_in = 0

def login(name):
    for i in admin:
        if name == i:
            print("Welcome in the system " + name)
            logged_in = 1
            break
    else:
        print('you are not an admin')

def set_user():
    if logged_in == 1:
        setu = 1
        if setu == 1:
            setu += 1
            print('we need some data')
            lastname = input('what is your lastname ')

login(input('fill in your name '))
set_user()

it results in this error:
Error:
Traceback (most recent call last):  File "python", line 24, in <module>  File "python", line 14, in set_user NameError: name 'logged_in' is not defined
Reply
#2
I'm not getting that error, at least not in Python 3.5. Note, however, that variables have scope. The logged_in variable in your login function only works within that function, it doesn't modify the logged_in variable on line 2. It's the difference between function scope and module scope. I would suggest using return values to modify the outer logged in variable.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
ichabod thanks verry much, this helped. but if i return a value, is it possible to make that a variable?
Reply
#4
Sure, here's a simple example:

def square(n):
    return n ** 2

four = square(2)
Now the four variable has the value returned by the square function.

So have the login function calculate and return the value of logged_in. I would then pass that in to the set_user function as a parameter. That's generally the way you want to do things: if a variable is needed inside a function, pass it as a parameter. If a variable inside the function is needed outside, return it from the function. This avoids scope problems, makes things easier to understand, and makes things easier to debug.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
thanks so much i just figured out that you can also do it by using global but this is what i was looking for.
Reply
#6
I would recommend parameters and return values over the use of global. It leads to cleaner, easier to maintain code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
but what would you do if you get more than one variable from one function
Reply
#8
(Oct-10-2016, 07:42 PM)Jordy Wrote: but what would you do if you get more than one variable from one function

you could add them to your 'return', creating a 'tuple' which you can call using it's element number. Such as

def num(num1, num2):
    add_num = num1 + num2
    mult_num = num1 * num2
    return add_num, mult_num
ans = num(2, 3)
print(type (ans))
print(ans[0])
print(ans[1])
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#9
Quote:creating a 'tuple' which you can call using it's element number.
Or just unpack to two variables:

def num(num1, num2):
    add_num = num1 + num2
    mult_num = num1 * num2
    return add_num, mult_num
    
add, mul = num(2, 3)
Reply
#10
Going back to your original post, in addition to what ichabod801 pointed out, lines 10 and 11 are not indented properly. Also, in line five you are iterating through the list 'admin' and can be simplified to

def login(name):
    if name in admin:
        print("Welcome in the system " + name)
        logged_in = 1
    else:
        print('you are not an admin')
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Lost Control over VLC jrockow 8 1,089 Jul-18-2023, 06:04 PM
Last Post: jrockow
  Lost Modules standenman 2 729 Jun-22-2023, 12:18 PM
Last Post: standenman
  Lost Module standenman 10 2,854 Oct-30-2021, 05:11 PM
Last Post: deanhystad
  XML Editing formatting lost ateestructural 2 1,918 Apr-08-2021, 04:41 AM
Last Post: ndc85430
  List structure lost when multiplying Protonn 2 2,248 Apr-23-2020, 04:16 AM
Last Post: buran
  lost dictionary jjpy 1 1,896 Jul-10-2019, 12:19 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020