I am making a player roster right now. When you start the program it gives you a list to choose from.
print(f"Welcome to the Team Roster Orginizer. Please use the menu values provided below!:")
print(f"_____________________________________________________________________________________________________________________________________________________________________________\n")
print(f"(1) [Add your chosen player to your roster and make your star team.]\n(2) [Display your roster of top notch players.]\n(3) [Exit program.]\n")
print(f"_____________________________________________________________________________________________________________________________________________________________________________\n"
player_names = []
while True:
Menu = int(input('Your Choice: '))
if Menu == 3:
print("Thanks for using this program! May your decisions bring you many winnings!\n")
break
if Menu == 1:
User_input = input("Please type a player to add them to your team. Your choices decide your future so choose wisely!: ")
player_names.append(User_input)
if Menu == 2:
print("Your Winning Team Roster:")
for Menu1 in player_names:
print(Menu1)
Here is where I am having trouble. The code itself works, the user enters 1 and the program asks for a player name. After the name is entered the system never asks for a new prompt but is expecting either 1, 2, or 3 to be entered.
What I am trying to have is the first name is entered and then another string is printed- "Please enter a new player to your roster or type (2) to post your roster and (3) to quit the program"
I want this question posted after each user entry until they decide on 2 or 3, how would I do this to ensure a more user-friendly experience?
Thanks
CodePudding user response:
If I understand correctly, I suggest putting another while loop in the '1' option to keep inputting names until they type a 2 or 3.
You would also need to put the '1' option first so that when you escape the loop you would still go to the '2' and '3' options.
If you wanted the default to be inputting names unless there is a '2' or '3' then I would check if the original input is a '2' or '3' and otherwise add it to the list.
CodePudding user response:
Instead of using a while loop for the whole thing, I only implemented the loop for the first option and broke the loop when one of the other options was chosen. I also wrote the code out in a way that is easier to read (for me). With bigger projects, having more readable code is quite useful.
player_names = []
print(f"Welcome to the Team Roster Orginizer. Please use the menu values provided below!:")
print(f"_____________________________________________________________________________________________________________________________________________________________________________\n")
print(f"(1) [Add your chosen player to your roster and make your star team.]\n(2) [Display your roster of top notch players.]\n(3) [Exit program.]\n")
print(f"_____________________________________________________________________________________________________________________________________________________________________________\n")
def add_player(x): # Add x to the player_names list
player_names.append(x)
def quit_game(): # Place your quit game code here
print('Thanks for playing the game')
def post_roster(): # Place your post roster code here
for val in player_names:
print(val)
Menu = input('Your Choice: ') # Get the input (1, 2, 3)
if Menu == '1': # If you choose to add a player run this code
add_player(input('Player Name: ')) # adds the player
while True: # Loops until the loop is broken
# enter your option
option = input('Press 2 to post roster, Press 3 to Quit game, Enter name to add a player\nYour Choice: ')
# Break loop and set menu to 2
if option == '2':
Menu = '2'
break
# Break loop and set menu to 3
if option == '3':
Menu = '3'
break
add_player(option) # Add anything that was typed into option if 2 or 3 was not selected
# After the loop is broken run this code to either post_roster or quit_game
if Menu == '2':
post_roster()
if Menu == '3':
quit_game()
To my understanding of the question, I believe this is the answer which you are looking for. I hope this helps.