Creating a new user account.

★★★

A gamertag is a persona used to identify players in online games. A gamertag can include letters, numbers and symbols, but no two players can share the same gamertag. This program allows a player to enter their preferred gamertag and prevent other players from claiming it.

Gamertag

Make

Write a program that asks the user to enter a gamertag. If the gamertag already exists in a file an error message is output, otherwise the gamertag is appended to the file.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called check_exists that:

  1. Takes a parameter that is the gamertag.
  2. Attempts to open a file called, players.txt for reading data. Note the file is included in the Trinket above for you to use as source data.
  3. Checks to see if the gamertag exists in the file.
  4. Returns True if it does and False if it does not.

Complete the subprogram called write_gamertag that:

  1. Takes a parameter that is the gamertag.
  2. Opens a file called, players.txt for appending data.
  3. Writes the gamertag to the file.

Complete the subprogram called get_gamertag that:

  1. Asks the user to enter their chosen gamertag.
  2. Calls, the check_exists function to determine if the gamertag is already taken.
  3. If the gamertag has already been taken it outputs a message, “Sorry, that gamertag is already taken. Try again.”
  4. If the gamertag has not already been taken it calls, write_gamertag to record the gamertag.
  5. Outputs a confirmation message.

Complete the main program so that:

  1. It calls the get_gamertag subprogram.

Typical inputs and outputs from the program would be:

players.txt file:

FluidYosta
IdolizedMero
TimotheosDark
DemonCamping
Enter your chosen gamertag: DemonCamping
Sorry, that gamertag is already taken. Try again.
Enter your chosen gamertag: DemonRushing
Welcome DemonRushing
Enter your chosen gamertag: OnlyUseMePistol
Welcome OnlyUseMePistol

Restricted automated feedback

Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.

🆘 If you're really stuck, use this Parsons code sorting exercise
check_exists
# Check if the gamertag has already been taken
def check_exists(gamertag):
---
    exists = False
    player = " "
---
    # Trap file not found
    try:
---
        file = open("players.txt", "r")
---
    except FileNotFoundError:
---
        return False
---
    else:
---
        # Check each gamertag in the file until found or eof
        while player and not exists:
---
            player = file.readline()
---
            player = player.strip()
---
            # Gamertag found in file
            if player == gamertag:
---
                exists = True
---
        file.close()
---
    return exists
write_gamertag
# Append the new gamertag to the file
def write_gamertag(gamertag):
---
    file = open("players.txt", "a")
---
    gamertag = gamertag + "\n"
---
    file.write(gamertag)
---
    file.close()
get_gamertag
# Ask the user to choose a new gamertag
def get_gamertag():
---
    exists = True
---
    # Only allow gamertags that have not been taken
    while exists:
---
        gamertag = input("Enter your chosen gamertag: ")
---
        exists = check_exists(gamertag)
---
        # Tell the user the gamertag has been taken
        if exists:
---
            print("Sorry, that gamertag is already taken. Try again.")
---
    write_gamertag(gamertag)
    print("Welcome", gamertag)