2

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?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
bobku
  • 23
  • 3
  • 1
    The rule for editing a text file, is that you should read one file, write in a new file either the original or modified lines, and then rename the new file with the original name. You could have a look at https://stackoverflow.com/q/55030437/3545273 or https://stackoverflow.com/q/16604826/3545273 – Serge Ballesta Apr 30 '21 at 08:13
  • Thank you! I will uphold that rule in my future programs. I appreciate the help! – bobku Apr 30 '21 at 13:26

2 Answers2

1

Try this :

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"))

            for index, line in enumerate(serverSearch):
                if duelistID in line:
                    serverSearch[index]=(serverSearch[index]).replace(duelistID,newUsername)

            with open("Duelist.txt","w") as file:
                file.write('\n'.join(serverSearch))

            if choice == "n":
                pass

            print("Exiting Edit Server Username...")

changeUsername(True,"Kaibaman",True,"Amazon")

I personally prefer loading the whole text file to a list, make the required changes and write back the whole list to the file.

Pervez
  • 391
  • 1
  • 10
  • Thank you so much! You got me over my hurdle, I did a little modification to your answer to make the function work how I wanted! I really appreciate the help, I just started my stack overflow account so I'm pretty hype that there's awesome people like you on here! – bobku Apr 30 '21 at 08:31
0

I think I need to rephrase my Question but Pervez was able to make the changes necessary for what I wanted. I just added a little more adjustments to come up with the following:

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"))

            for index, line in enumerate(serverSearch):
                if duelistID in line:
                    if search == serverSearch[index+1]:
                        serverSearch[index+2]=(serverSearch[index+2]).replace(serverSearch[index+2],username)

            with open("Duelist.txt","w") as file:
                file.write('\n'.join(serverSearch))

            if choice == "n":
                pass

            print("Exiting Edit Server Username...")

In this case, the function will only replace the text in Duelist.txt if the function can match the duelistID and search lines. Hopefully I explained this right.

bobku
  • 23
  • 3