Python Challenge Solution

fahad lashari
fahad lashari
6,139 Points

My Approach

player_names = []

def print_player_list(list):
    print("There are {} players on your team:".format(len(list)))
    for i in list:
        print("Player {}: {}".format(list.index(i)+1, i))

    #for (num, i) in enumerate(list):
        #print("Player {}: {}".format(num+1, i))

def user_input_prompt():
    try:
        user_inputYN = input()
        if user_inputYN not in ["y", "Y", "n", "N"]:
            raise ValueError("Please only enter: Y or N")
    except ValueError as err:
        print(err)
        # there needs to be a return on the function call here as the else returns the value to 
         #this function call and not adding a 'return' means there is no value being returned
        return user_input_prompt()
    else:
        #print("Before getting returned: ", user_inputYN)
        return user_inputYN

print("Would you like to add players to the team? \nEnter Y/N: ")
user_inputYN = user_input_prompt()
#print("After being assigned: {}".format(user_inputYN))
while user_inputYN in ["y", "Y"]:
    new_player = input("please enter the name of the player:\n").capitalize()
    player_names.append(new_player)
    print("Would you like to add more players to the team? \nEnter Y/N: ")
    user_inputYN = user_input_prompt()
    if user_inputYN == "n":
        print_player_list(player_names)
        goal_keeper = int(input("Please select the goal keeper by selecting the player number select from {}-{}:\n".format(1, len(player_names))))
        print("Great choice!!! {} will be the goal keeper. Have a great game! :D".format(player_names[goal_keeper-1]))
else:
    print("Have a great day! goodbye :)")

Please ignore some debugging code I've put in there as I kept running into a few issues. Would appreciate any suggestions or feedback on how to improve.

regards =)

1 Answer

Good job. One comment: You prompt for uppercase N but then only check lowercase here:

if user_inputYN == "n":

As a result the goal keeper code doesn't execute if a valid answer of N is entered.

fahad lashari
fahad lashari
6,139 Points

Ahhh nice catch. Thank you very much :)