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.
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.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
check_exists that:players.txt for reading data. Note the file is included in the Trinket above for you to use as source data.write_gamertag that:players.txt for appending data.get_gamertag that:check_exists function to determine if the gamertag is already taken.write_gamertag to record the gamertag.get_gamertag subprogram.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
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)