I am new to programming so If there's an easier way to go about this, please let me know. There is a text file called Duelist.txt
that contains the following:
-------------------------------------------------
Duelist: Seto Kaiba
Server: Amazon
Username: Kaibaman
Password: Blue-Eyes White Dragon
-------------------------------------------------
Duelist: Mokuba Kaiba
Server: Amazon
Username: Kaibaboy
Password: KaibabestBro
------------------------------------------------
This changeUsername(login,duelistID,exists,server):
function runs if login
and exists
are TRUE
. If they want to make the change the username it prompts the user for new username and then the function is supposed to change the username in the Duelist.txt
two lines after the current location, but it does not...
def changeUsername(login,duelistID,exists,server):
index = 0
username = "Username: "
serverSearch = []
duel = "Duelist: " + duelistID
search = "Server: " + server
if login == True and exists ==True:
print("Would you like to change this username for " + server + "?")
print("Press \"y\" key for yes or press \"n\" key for no followed by the Enter key")
choice = input(f'{duelistID}:')
if choice == "y":
newUsername = input("Please enter new username: ")
username += newUsername
with open("Duelist.txt", 'r') as file:
content = file.readlines()
for line in content:
serverSearch.append(line.strip("\n"))
with open("Duelist.txt", 'r+') as file:
for index, line in enumerate(serverSearch):
if duel in line:
if search == serverSearch[index+1]:
serverSearch[index+2] = username
file.write(line.replace(serverSearch[index+2], username))
if choice == "n":
pass
print("Exiting Edit Server Username...")
time.sleep(1)
This is the user input in the function changeUsername(login,duelistID,exists,server)
with login
and exists
as True, duelistID
as Mokuba Kaiba, and server
as Amazon.
Would you like to change this username for Amazon?
Press "y" key for yes or press "n" key for no followed by the Enter key
Mokuba Kaiba:y
Please enter new username: Mokuba
Exiting Edit Server Username...
After the user input I get the following in the Duelist.txt
Duelist: Mokuba Kaiba---------------------------
Duelist: Seto Kaiba
Server: Amazon
Username: Kaibaman
Password: Blue-Eyes White Dragon
-------------------------------------------------
Duelist: Mokuba Kaiba
Server: Amazon
Username: Kaibaboy
Password: KaibabestBro
-------------------------------------------------
I've had a hard time trying to figure this out and make a simple solution but I'm just a third rate coder with a fourth rate code. Can anyone help make this function do what it's supposed to do?