+ 1
login system
Check please my code and tell me what's wrong? I'm trying to make a login system, but when I run it only runs the status input and which choice i choose(y/n or q) does not do anything.Sorry for my english! :) stored = {} status = () def choiseDisplay(): status = raw_input("are you already registered? y/n? press q to quit\n") if status == "y": oldUser() elif status == "n": newUser() elif status == "q": print "quit" while status != "q": choiseDisplay()
5 Answers
+ 6
"""
@Andres Eduardo Goncalves is right for indentation in all cases (versions of Python 2 or 3): without indenting all your if/else statements, this will not be part of the function definition...
Anyway, @Don advices are perfect:
+ 'print' parenthesis (rounded brackets) are mandatory in Python 3, not in 2
+ raw_input() exist only in Python 2 (and is mandatory to get string user input) and shoud be replaced by input() in 3 (returning string in all cases, contrarly to Python 2 version)
+ global variables are only read allowed by default inside other scopes (ie. your function scope). To be able to read/write it, you need to declare it 'global' in the function scope (else assignement will create a local variable and not update your global one)
+ initialization of your 'status' variable would be more clean by setting an empty string rather an empty tuple (but this is not blocking)
So, fixed code in Python 3 should be (with fake oldUser() and newUser() functions:
"""
def oldUser():
print('old user')
def newUser():
print('new user')
stored = {}
status = ""
def choiseDisplay():
global status
status = input("are you already registered? y/n? press q to quit\n")
if status == "y":
oldUser()
elif status == "n":
newUser()
elif status == "q":
print("quit")
while status != "q":
choiseDisplay()
# and just replace 'input' by 'raw_input' to be working in Python 2 ;)
"""
Anyway, if you try to run it on code playground, it must be Python 3 version, and you must know limitations of user inputs: it's mandatory to give all the code required user input at once just before running script (and you get output at once at very end) becase scripts are ran on server side, not locally... So, to test your code in playground, you have to type somthing like:
y
n
q
... to be able to make it run without errors :P
But you can also improve your code to handle upper/lower case and/or handle case of unexpected input (not 'q', 'y' or 'n') ;)
"""
+ 2
Well it's quite obvious you're using Python 2 and not 3 by looking at the function raw_input. I strongly recommend you cross over to Python 3 it's the future of the language. Anyways...
Change status to be an empty string. Why are you making a tuple? So:
status = ''
Now. You have to remember that functions have different Scopes than the main file. Every variable that's created inside of the function is local and only can be accessed inside of that function. You're trying to change the global variable so you must explicitly state that you are.
def choose_display():
global status
status = raw_input()
and the rest carries over. Again, off of your snippet of code i would assume you have newUser and oldUser already defined.
0
you didn't ident the if/elif statements
print is a function, you must write print("quit")
0
Thank you very much all for your answers! @visph Of course the code will improved this is the start but i was confused with the def function and i didn't know how to declare it.So,from what i understood first i declare def function then i tell what it will do and then i call it right?
@Don Thanks for your answer ! when i start learn python i read in the internet that python 2 is better :P ok i will continue in python 3 :)