简体   繁体   中英

Once I have one username in my file, the process dies (Python)

The entire program works fine if I have one username in the users.csv file, but as soon as a second is added repl (IDE) quits the program. I know the code is very messy and amateur but for now I'm just looking to fix this part.

Part that needs to be altered v

def login():
  existing = input("Do you already have a account? Y/N >" ).upper()
  if existing == "Y":
    pass
  else:
    print("Welcome to the regristration page")
    file = open("users.csv", "a+")
    file.write("{}\n".format(input("What would you like your username to be? >")))
    file.close()
login()

def auth_users():
  username = input("What is your username?")
  file = open("users.csv","r")
  reader = csv.reader(file)
  for record in reader:
    if record[0] == username:
      continue
    else:
      exit()
  file.close()
auth_users()

Program in its entirety

def login():
  existing = input("Do you already have a account? Y/N >" ).upper()
  if existing == "Y":
    pass
  else:
    print("Welcome to the regristration page")
    file = open("users.csv", "a+")
    file.write("{}\n".format(input("What would you like your username to be? >")))
    file.close()
login()

def auth_users():
  username = input("What is your username?")
  file = open("users.csv","r")
  reader = csv.reader(file)
  for record in reader:
    if record[0] == username:
      continue
    else:
      exit()
  file.close()
auth_users()

There is no error while running your program. Either way, no matter if the user exists in your file or not, your program will end without output.

You can try to improve things a bit:

def auth_users():
  username = input("What is your username?")
  file = open("users.csv","r")
  reader = csv.reader(file)
  for record in reader:
    if record[0] == username:
      print(f"Hello, {username}")
      exit() # You found the guy, exit your program here
    # Don't exit here: go through all the names until you find the guy (or not)
  # End of the loop. We didn't find the guy.
  print("You're not registered")

Here is the problem

for record in reader:
  if record[0] == username:
    continue
  else:
    exit()

You are probably mistaken in your use of exit() and continue . The exit function is usually called when you want to exit out of python's interactive mode and raises the SystemExit exception (in this case causing your program to exit). continue on the other hand tells python to move on to the next step in the loop.

You probably want to do something like this instead:

for record in reader:
  if record[0] == username:
    # Handle authenticated users here
    print("Login successful")
    return # exit the function

# Handle unauthenticated users here
print("User not found")

You should also consider replacing your file opening and closing with context managers. Instead of:

my_file = open("some-file", "r")
# read some input from the file
my_file.close()

# ...

my_file = open("some-file", "w")
# write some output to the file
my_file.close()

use:

with open("some-file", "r") as my_file:
  # read my_file in here

# ...

with open("some-file", "w") as my_file:
  # write to my_file in here

This way python tries to close your file even if an exception is encountered along the way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM