Nukestye
Nukestye

Reputation: 19

Functions not being called python

Function is not being called inside another function:

ch = False
while not ch:
     print("""
        1. Make a User
        2. Login in and Play
        3. Quit

   """)
     a = input("What would you like to do: ")
     if a == '1':
        un_maker()
     elif a == '2':
        player1Login()
     elif a == '3':
        input('\nEnter to exit...\n')
        quit()

Where when a is 2, it should move on to player1Login(), it does but it does not go to the next function inside the player1Login().

The code for player1Login() and the function it's supposed to run:

 def player1Login():
     """ Login for player 1 """
     user1 = input("Please enter your usernames[Caps sensitive]:  ") # Asking the 
     user to input there username
     pass1 = input("Please also enter your password[Caps sensitive]: ") # Asking the user to input there password
     verfi1(user1, pass1)

  def verfi1(user1, pass1):
     """ Verfications of the user """
     with open("data.csv", "r") as f:
        reader = csv.reader(f) # makes 'reader' the csv reader of file 'f'
        for row in reader: # Looking at rows/records inside the file
            if user1 in row: # Looks for username inside the row
                if pass1 in row[1]:
                     print("Player 1 Confirmed") 
                     player2Login()
                elif pass1 != row[1] or reader == '':
                     print("""You have entered the wrong Username/Password for Player 1
                              This could be due to:
                              1. Inputed the wrong username/password
                              2. Inputed the wrong case in username/password
                              3. Inputed username/password that does not exit
                           """)
                     break
            else:
                 #print("Reader")
                 next(reader)

Basically, the code should output when a is 2, player1Login() then move onto verfi1() function however it does not, it just goes back to the menu.

Answer Found:

def menu():
    ch = False
    optin = 0
    while not ch :
           print("""
                1. Make a User
                2. Login in and Play
                3. Quit
                """)
           optin = input("What would you like to do: ")
           if optin == '1':
               un_maker()
           elif optin == '2':
               player1Login()
           elif optin == '3':
               input('\nEnter to exit...\n')
               quit()

def player1Login():
""" Login for player 1 """
    user1 = input("Please enter your usernames[Caps sensitive]:  ") 
    pass1 = input("Please also enter your password[Caps sensitive]: ")
    boop(user1, pass1)

def boop(user1, pass1):
    with open('data.csv', 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            if pass1 in row[1] and user1 in row[0]:
                 print("Username and Password found, Player 1 Confirmed")
                 player2Login(user1)
            elif pass1 not in row[1] and user1 not in row[0] and row[0] == '' :
                 print("Player Not found / Password incorrect")
                 menu()

Upvotes: 1

Views: 425

Answers (1)

Mike Müller
Mike Müller

Reputation: 85492

You skip lines with next and therefore jump over each other user in you data.csv file. This can lead to that if user1 in row is never true even though user1 is in your file but happens to be on a skipped line.

Your code:

for row in reader: # Looking at rows/records inside the file
    if user1 in row: # Looks for username inside the row
        if pass1 in row[1]:
            print("Player 1 Confirmed") 
            player2Login()
        elif pass1 != row[1] or reader == '':
            print("""You have entered the wrong Username/Password for Player 1
            This could be due to:
            1. Inputed the wrong username/password
            2. Inputed the wrong case in username/password
            3. Inputed username/password that does not exit
            """)
            break
    else:
        next(reader) # this skips the next

Remove the elseclause because for row in reader already goes over all lines.

Upvotes: 2

Related Questions