I am creating an interactive game but I am stuck on getting the loop of 'games' to work correctly.
I have asked the user for input to determine the number of loops. The variable is an integer and the only numbers accepted are 1-5 inclusive. What I am expecting is if the user enter 2 then the game loops twice (unless both characters die by way of the health declining to 0).
There are other options in the game such as resetting character health etc and then the user can go back to the battle option and again enter the number of games they want to play.
What I am finding is:
- sometimes the number of battles played matches what the user enters BUT sometimes it doesn't work (i.e. user enters 2 but it plays 4 games)
- it never works if the user enters 1 (I have tried changing the line around number_rounds != 1 to 0 but that doesn't work either
I have only pasted the relevant snippet of code in hope that someone who is more familiar with Python can spot the issue. Thanks in advance.
# Defining the function to display highest battles won
def do_battle(character_list, opponent1_pos, opponent2_pos):
# Requesting user input to select number of rounds to be played
number_rounds = int(input('Please enter number of battle rounds: '))
# While loop to validate input is a number between 1-5 inclusive
while number_rounds < 1 or number_rounds > 5:
print('Must be between 1-5 inclusive.')
number_rounds = int(input('Please enter number of battle rounds: '))
player1_health = character_list[opponent1_pos][-1]
player2_health = character_list[opponent2_pos][-1]
round_number = 0
for i in range(number_rounds):
while number_rounds != 1 and player1_health > 0 and player2_health > 0:
round_number = round_number 1
damage1 = random.randint(0, 50)
player1_health = max(0, player1_health - damage1)
character = character_list[opponent1_pos]
character[-1] = player1_health
damage2 = random.randint(0, 50)
player2_health = max(0, player2_health - damage2)
character = character_list[opponent2_pos]
character[-1] = player2_health
CodePudding user response:
user enters 2 but it plays 4 games
The problem is your while
loop inside the for
loop - that is why you get more rounds than intended, because you are looping within the loop.
If I understood the intent correctly, what you want is to play up to number_rounds
rounds and then stop when one of the players' health reaches zero?
In that case what you really want there is just an if
statement.
Also, you don't need the round_number
variable, since you have i
from the for loop. And I don't see the point of while number_rounds != 1
either.
My attempt at a fixed version would look like:
# Defining the function to display highest battles won
def do_battle(character_list, opponent1_pos, opponent2_pos):
# While loop to validate input is a number between 1-5 inclusive
number_rounds = 0
while True:
number_rounds = int(input('Please enter number of battle rounds: '))
if 1 <= number_rounds <= 5:
# exit the loop if they entered a valid value
break
print('Must be between 1-5 inclusive.')
player1_health = character_list[opponent1_pos][-1]
player2_health = character_list[opponent2_pos][-1]
for i in range(number_rounds):
print(f"Round #{i 1}")
damage1 = random.randint(0, 50)
player1_health = max(0, player1_health - damage1)
character = character_list[opponent1_pos]
character[-1] = player1_health
damage2 = random.randint(0, 50)
player2_health = max(0, player2_health - damage2)
character = character_list[opponent2_pos]
character[-1] = player2_health
# exit the battle if any of the players died
if player1_health == 0 or player2_health == 0:
break