-1

I am trying to complete the signing in process of my code in Python in Repl.it. It will search the csv file called "UDfile" to confirm usernames and passwords so then it can access the card game I'm creating. Here is what the file looks like:

markd,12345
pltwo,67890
forhb,10\/3

However, I am creating this for 2 users. Logging in the first person works fine, so long as the username I input is above the second's username. But when I try to input the second username, correct or not, it will always say it doesn't recognise the username. I suspect it doesn't do a complete search. How can I get it thoroughly searching this csv file so that it'll allow me to put in P2's password and therefore continue the program?

I've searching stackoverflow for something along the lines of "How to get this code to find the existing username when it says it isn't there?" but there was nothing I could find.

I also tried adding

sys.exit

to the end of

print("Username not found")

in user_findtwo(file, user), hoping it would search the file thoroughly and terminate only if it completely couldn't find it, but that will immediately end the code after the program says it isn't found once.

Finally, I tried a while loop to say

while row[0] != usertwo:
 print("Username not found")
sys.exit()

I hoped it would just search the whole text file and say "Username not found" until it found player two's username, but instead that just endlessly prints "Username not found".

import csv
import sys

def main():
  login()
  logintwo()
  show_menu()
  menu_choice = int(input("Welcome to the RYG card game! Press \"1\" and then \"Enter\" to play a game:"))
  options(menu_choice)

def login(): 
  with open("UDfile", "r") as file:
    file_reader = csv.reader(file)
    user_find(file_reader)
    file.close

def user_find(file):
  user()
  for row in file:
   if row[0] == user:
    print("Username is found:", user)
    user_found = [row[0], row[1]]
    pass_check(user_found)
    break
  else:
      print("Username not found")
      sys.exit()

def user():
  global user
  user = input("Player 1, enter your username (must be above player 2's desired name):")

def logintwo():
  with open("UDfile", "r") as file:
    file_reader = csv.reader(file)
    user_findtwo(file_reader, user)
    file.close

def user_findtwo(file, user):

  usertwo = input("Player 2 enter your name:")
  if usertwo == user:
    print("Usernames must be different. Restart the program.")
    sys.exit()
  else:
   for row in file:
      if row[0] == usertwo:
       print("Username is found:", usertwo)
       user_found = [row[0],row[1]]
       pass_checktwo(user_found)
       break
      else:
         print("Username not found")

def pass_check(user_found):
  x = True
  while x:
   user = input("Enter your password:")
   if user_found[1] == user:
    print("Password match.")
    x = False
   else:
    print("Password doesn't match.")
    sys.exit()

def pass_checktwo(user_found):
  y = True
  while y:
    usertwo = input("Player 2 enter your password:")
    if user_found[1] == usertwo:
      print("Password match.")
      y = False
    else:
      print("Password doesn't match.")
      sys.exit()

See background for expected and actual results.

MrMD
  • 1
  • 1

1 Answers1

0

Could you use this method to loop through the rows? you could use len(csvFileArray) to get the length, have it loop to a maximum of that to avoid index out of bounds, and could have it pass the function once it finds what it needs (so it does not keep looping after?)

https://stackoverflow.com/a/39629619

just a fast thought

AlbinoRhino
  • 467
  • 6
  • 23