-1

So, noob here, now that's out of the way let's get to the problem shall we? (First post, ever)

So, my code (below) is quite inconsistent. I have just begun, about two days ago, and I'm dying to become better and love all kinds of critique, so unload all you've got.

When the user enters "login" to login he gets prompted to enter the username (a separate function creating a global variable for the password function to use as the welcoming name.) and is immediately redirected to the password check. Here there are two outcomes: 1. The user types in the correct password, the user will be "let in:" "Welcome " + username + "." 2. The user types in the wrong password/types anything that isn't the password: the user is sent back to the username check. But what happens for me is that the Python instead jumps back to the intro code and exits the program with: "Invalid input." And it seems as if the program goes to the password function, does its thing and immediately reverts to the former function.

Tl:Dr: Functions don't loop as they should, instead return to the previous function, thus ignoring the action I wrote.

Excuse me for the wall of text, and please, please please for the love of god or anything, don't hit me with a wall of code and "Fixed!" I'm very new and barely understand anything outside this patch of code which I myself wrote with my new gained experience. Otherwise it will end up with a [ctrl + c --> ctrl + v] situation and I really, really want to learn, not copy. So if you think you have what it takes, please do your best, everything is appreciated :)

Ps: Tips & Tricks are valuable for me! PPs: If you want more code, just say it and I'll do my best.

Changes in the login screen:

  • choice = input

if input blah blah: "function"

  • creating a whole function for it
  • moving the commands around
  • messing with other codes
# Password check.
def password_check():
   print("Please enter your password")
   print("")
   password = input("")
if password == "1234":
   return "Welcome " + username + "."
elif password != "1234":
   username_check()

# Intro screen: invalid input notifier. 
if choice != ["login", "exit"]:
   print("Invalid input.")
   input(" ")
Right leg
  • 16,080
  • 7
  • 48
  • 81
  • 1
    I'm pretty sure `choice` will never be equal to `["login, "exit"]`. You probably want `choice not in ["login", "exit"]`. – Right leg Sep 11 '19 at 15:00
  • The `password_check()` doesn't return anything because your indentation is off. – alex Sep 11 '19 at 15:05
  • @Rightleg Yes about that, choice = input(""). It was the variable to navigate in the menu, which had the options -login and -exit. – PythN00b Sep 11 '19 at 15:52

1 Answers1

0

Your if statement checking password was in the global area and not inside your function.

# Password check.
def password_check():
    password = input("Please enter your password\n\n ")
    if password == "1234":
        return "Welcome " + username + "."
    elif password != "1234":
        username_check()

# Intro screen: invalid input notifier. 
if choice != ["login", "exit"]:
   print("Invalid input.")
   input(" ")

ShayneLoyd
  • 633
  • 1
  • 4
  • 9