Okay, so up to the file creation/writing the code works as expected. I'm trying to figure out how to write the output to a text file and have it saved so I can add a list of users by just adding their names.
def login():
while True:
first_name = input("Enter your first name: ")
if first_name.lower() == "done":
print("Done!")
break
last_name = input("Enter your last name: ")
Username = str(first_name[0]) str(last_name)
Password = str(first_name[0]) str(last_name) str('1!')
print("Username: ", Username.upper())
print("Password: ", Password.upper())
print("\n")
login()
with open('test.txt', 'w') as f:
for lines in login():
f.writelines(lines)
I just turned it into the function "login" as to my knowledge I needed a way to tell the file what to write.
The output of the first 13 lines (enter first & last name, print username & password) looks like this...
" Enter your first name: Tony Enter your last name: Stark Username: TSTARK Password: TSTARK1!
Enter your first name: john Enter your last name: wayne Username: JWAYNE Password: JWAYNE1!
Enter your first name: done Done! Enter your first name: "
As to what is preferred to be written in the file:
" Username: TSTARK Password: TSTARK1!
Username: JWAYNE Password: JWAYNE1!
"
would be ideal to have it in a similar format with a \n to separate each user's information for ease of access
In terms of what I've tried to resolve myself. I'm not too familiar with file writing code I did use https://www.pythontutorial.net/python-basics/python-write-text-file/ as a resource. I tried creating the file on my desktop to see if that might have been the issue, I was writing to a non-existent file. I believe it will require the writelines method as I'm attempting to essentially write 2 lines as 1 entry and then add a blank line and start the second entry.
I know it's attempting to open a text file named test.txt with the permission to write. I assume line 16
for lines in login():
is where it should be grabbing the username & password from the function "login" and then directed to write multiple lines
CodePudding user response:
try this.
def login():
lst = []
while True:
first_name = input("Enter your first name: ")
if first_name.lower() == "done":
print("Done!")
break
last_name = input("Enter your last name: ")
Username = str(first_name[0]) str(last_name)
Password = str(first_name[0]) str(last_name) str('1!')
print("Username: ", Username.upper())
print("Password: ", Password.upper())
print("\n")
lst.append(f"Username: {Username.upper()} Password: {Password.upper()}\n") # append value to lst
return lst
# --------------------------------------------------------------- #
with open('test.txt', 'a') as f:
f.writelines(login()) # write return lst to file mode is `a` for append
# OR #
return_lst = login()
with open('test.txt', 'a') as f:
f.writelines(return_lst) # write return lst to file mode is `a` for append
THis one after OP want(discussion in a private chat group)
def login():
while True:
with open('test.txt','a') as f:
first_name = input("Enter your first name: ")
if first_name.lower() == "done":
print("Done!")
break
last_name = input("Enter your last name: ")
Username = str(first_name[0]) str(last_name)
Password = str(first_name[0]) str(last_name) str('1!')
print("Username: ", Username.upper())
print("Password: ", Password.upper())
print("\n")
f.write(f"Username: {Username.upper()} Password: {Password.upper()}\n")
login()
CodePudding user response:
You could print with file input
with open('test.txt', 'w') as f:
def login():
while True:
first_name = input("Enter your first name: ")
if first_name.lower() == "done":
print("Done!", file=f)
break
last_name = input("Enter your last name: ")
Username = str(first_name[0]) str(last_name)
Password = str(first_name[0]) str(last_name) str('1!')
print("Username: ", Username.upper(), file=f)
print("Password: ", Password.upper(), file=f)
print("\n")
login()