0

So here is the code if run the begin function it will just run option1 and not print the other stuff like i want to make this login thing work but it won't work for some reseon.

def login():
    print ("You are now logged in")

def reg():
   file = open ("User_details","a")
   file.write("\n"+"Ussername"+","+"Password"+","+"Age"+","+"Real_name"+","+"Last_name"+","+"Email")
   file.close

def Access(option):
    if option == "login":
        input ("Ussername: ")
        input ("Password: ")
        input ("Age: ")
        input ("Real name: ")
        input ("Last name: ")
        input ("Email: ")
        login()
    else:
        print ("Enter all the info we will be asking you to register")
        input ("Ussername: ")
        input ("Password: ")
        input ("Age: ")
        input ("Real name: ")
        input ("Last name: ")
        input ("Email: ")
        reg()

def begin():
     
    print ("Hi my name is Steve would you like to register or login")
    option1 = input ("Login or Register: ")
    if option1 != "login" and "register": 
        begin()
    if option1 == "register":
        Access()
    if option1 == "login":
        login()
begin()
Access()
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
DJDimsa
  • 1
  • 1
  • The line `if option1 != "login" and "register":` is not doing what you want. Python's boolean logic doesn't work that way. You need to do either `if option1 != "login" and option1 != "register":` or `if option1 not in ("login", "register"):`. [More info](https://stackoverflow.com/a/15112149/6273251) – Random Davis Feb 08 '22 at 18:40
  • You aren't saving any references to the strings the user is inputing. As a result, even if you were using variable names (and not hard-coded strings) in `reg`, you'd just get a `NameError`. – chepner Feb 08 '22 at 18:42
  • Also, don't use recursion to implement a potentially infinite loop. – chepner Feb 08 '22 at 18:43

1 Answers1

0

This is because your first conditional will always be True. For example, let's see how an example input would work:

>>> option1 = "unknown input"
>>> if option1 != "login" and "register": print("TRUE")
TRUE

# If we split the statement into pieces, we can see why
>>> option1 != "login"
True
# The part after the `and` isn't testing anything, and is always True!
>>> bool("register")
True
# Your `if` statement effectively tests option1 != "login"
>>> True and "register"
True

To fix this, you want to make sure that option1 isn't either of the strings. You probably want to clean up the input value a bit too, by stripping any whitespace.

option1 = input("Login or Register: ")
option1 = option1.strip()  # remove accidental whitespace from the ends

# The fixed `if` statement
if option1 != "login" and option1 != "register":
    begin()
damon
  • 14,485
  • 14
  • 56
  • 75