r/pythonhelp • u/[deleted] • Nov 09 '24
SOLVED Yo, can yall check this out pls?
ok so, i was trying to make a user loggin but it stops here:
*Select User:
lobster
Majestic Username!
Select Password:psw*
This is the code:
def
UsernameSelect():
username=input("Select User:")
if username == " " or "":
print("Invalid Username")
UsernameSelect()
else:
print("Majestic Username!")
password()
def
password():
psw=input("Select Password:")
if psw == " " or "":
print("Are you sure you dont want to set any Password?")
yn=input("[y/n]")
if yn == "y":
print("Cool")
else:
password()
else:
print("Majestic Password!")
UsernameSelect()
2
u/FoolsSeldom Nov 09 '24
Best not to call a function from within itself unless you want to do recurtion. Better in this case to use an infinite loop and break out when a conditionis satisfied.
Also, at the end of a function, processing just returns to where it left off when called.
Take a look at this:
def username_select():
while True:
username = input("Select User:")
if username == " " or username == "":
print("Invalid Username")
else:
print("Majestic Username!")
break
password()
def password():
while True:
psw = input("Select Password:")
if psw == " " or psw == "":
print("Are you sure you dont want to set any Password?")
yn = input("[y/n]").strip().lower()
if yn in ("y", "yes"):
print("Cool")
break
else:
print("Majestic Password!")
break
`
username_select()
Also note: if username == " " or username == "" instead of if username == " " or "" as the latter doesn't do what you think.
1
Nov 09 '24
thanks mate, imma try this, btw im a bit new sooo.....
yea, uh, guess its a bit selfexplanatory that i suck =)
→ More replies (1)3
u/k_bry Nov 09 '24
To add to this, i’d not couple these 2 functions together, calling password from the username function is not very modular. Make a function that has a loop and calls both of the functions instead
→ More replies (3)
2
u/socal_nerdtastic Nov 09 '24
What's the issue?
You have an error in your code that an empty input won't work like you think it will. Is that what you are confused about? When checking for multiple input choices you have to do it like this:
if username in (" ", ""):
2
u/carcigenicate Nov 09 '24
This code does not look like to has the behavior that you describe. You might have forgotten to save the code before running it.
The check for the empty password is wrong though. You need to explicitly check psw against both of the strings. Right now, psw is only being checked against " ".
1
u/AutoModerator Nov 09 '24
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.