Your Problem is because of Python's scopes
which are stated here (and below):
LEGB Rule.
- Local.
Names assigned in any way within a function, and not declared global in that function.
- Enclosing function locals.
Name in the local scope of any and all enclosing functions (def
or lambda
), form inner to outer.
- Global (module).
Names assigned at the top-level of a module file, or declared global in a def within the file.
- Built-in (Python).
Names preassigned in the built-in names module : open, range, SyntaxError
, etc
So, your x
variable is a variable local to main()
. So, to get the variable out of the local scope, you can get the global variable to the local scope too.
Your code seems to have various logical errors (and some NameError
s). I've tried to understand and have changed/reformed your code, to something that will work.
def menu():
global x
while x not in ('1','2'): # 1
print " Menu"
print "1) Login"
print "2) Under dev"
x = raw_input('Select menu option: ') # 2
if x in ('1','2'): # 1
break
x = ''
menu()
# 3
if x == '1':
y = raw_input("enter username: ") # 2
z = raw_input("enter password: ") # 2
if y != 'username' or z != 'password': # 4
print "username or password incorrect"
else: # 5
print 'You will be logged in'
elif x == '2':
print "Under development"
Other than the grammatical changes, lets see the changes on the logical part.(The numbers in comments refer to the point which elaborates on the change.)
raw_input
returns a string. So, it will never be 1
or 2
(integers).
Also, x != 1 or 2
will evaluate to True
as it is equivalent to (x != 1) or (2)
and 2
always has a True
value in Python.
raw_input
takes a prompt as an argument, which is displayed before taking the input from
- Simply put, the structure of your ifs and elifs is not correct.
y or z == False
is evaluated to (y) or (z = False)
And z
is never False
as it is a string.
So, if y
is not ''
, then this is evaluated to True, but that is not like something you want (it seems)..
- The code in
else
is executed if the if (and all the elifs) conditionals are False
.
Here we give the under development message.
More coming