Reputation: 359
so I am trying to prompt the user to assign a type to the player, but I'm not sure why the system asks the question twice before printing out the name of the bot. I'm not sure where the error comes from, which loop
Player Class:
def get_player_type(player_num: int) -> str:
"""input player type and check if valid """
while True:
type = input(f'Choose the type for Player {player_num}\nEnter Human or Random or Simple: ')
if type.strip().lower() == 'h' or type.strip().lower() == 'r' or type.strip().lower() == 's':
return type.strip().lower()
else:
raise ValueError(f'{type} is not one of Human or Random or Simple. Please try again.')
Simple AI Class:
class SimpleAI(Player):
def get_simpleAI_name(player_num: int) -> str):
"""declare names for simple AI"""
name = "SimpleAI " + str(player_num)
return name
Game Class:
def create_game(path_to_config_file: str) -> "Game":
for player_num in range(1, 3):
if Player.get_player_type(player_num) == 'h':
new_player = HumanPlayer.create_user(player_num, game_config.blank_char, player_list)
player_list.append(new_player)
elif Player.get_player_type(player_num) == 's':
new_AI = SimpleAI.get_simpleAI_name()
print(new_AI)
return Game(game_board, new_player, player_list)
My Output of the code:
Choose the type for Player 1
Enter Human or Random or Simple: s
Choose the type for Player 1
Enter Human or Random or Simple: s
SimpleAI 1
Correct Output should be:
Choose the type for Player 1
Enter Human or Random or Simple: s
SimpleAI 1
Upvotes: 0
Views: 28
Reputation: 6056
You are calling Player.get_player_type
everytime you try to check it against a value. What you should really do is call this method, store the result in a variable and compare that variable with your values:
player_type = Player.get_player_type(player_num)
if player_type == 'h':
new_player = HumanPlayer.create_user(player_num, game_config.blank_char, player_list)
player_list.append(new_player)
elif player_type == 's':
new_AI = SimpleAI.get_simpleAI_name()
print(new_AI)
Upvotes: 1