Python Forum
trouble writing to file after while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trouble writing to file after while loop
#1
I'm trying to write a register form for account creation. When using a while loop to check for username matches I notice the loop gets ran through multiple times therefore appending to the file multiple times. The file used is a simple text file containing a simple list of usernames and passwords. All I want is for the loop to check if the username input matches any username in the file, if so then it should prompt for a new username to be entered and then check if there is a match on that username. If there is no match then it should continue to let the user know the account was created and append the username and password to the end of the file.
###################################
########## Register File ##########
###################################

# Open usrdata for read to check for username, pw match########
usrinfile = open("usrdata", 'r')

usrdatalist = [] # Create a list to check

for line in usrinfile:
   un, pw= (line.split(','))
   usrdatalist.append([un, pw])
# Close file
usrinfile.close()
###############################################################

# Get information from user
username = input("Enter a username: ")
password = input("Enter a password: ")

# Check if username already exists
usroutfile = open("usrdata", 'a')
for name in usrdatalist:
   while (name[0] == username):
       username = input("Name taken, enter a new name: ")
   if (name[0] != username):
       print("Account creation sucessful!")
   usroutfile.write(username + "," + password + '\n')
usroutfile.close()
Reply


Messages In This Thread
trouble writing to file after while loop - by Low_Ki_ - Jan-04-2017, 03:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,307 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,410 Mar-30-2022, 06:25 AM
Last Post: bowlofred
Question Having trouble writing an Enum with a custom __new__ method stevendaprano 3 4,009 Feb-13-2022, 06:37 AM
Last Post: deanhystad
  Writing to file ends incorrectly project_science 4 2,641 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,654 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,335 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Trouble with reading csv file and putting it into a file Milfredo 3 2,218 Sep-04-2020, 05:30 AM
Last Post: Milfredo
  Failure in writing binary text to file Gigux 7 3,717 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,327 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020