Home > Enterprise >  Python program keeps asking question again and again
Python program keeps asking question again and again

Time:06-13

I am a python intermediate and tried to make a text based adventure game in python. There is a class called NPC which has the NPCs. In that class there is a method called attack() which will attack the player and the player can also attack or defend. It is position based so the user can enter 3 things - 'high', 'middle' and 'low'. This is the code.

turn = True
        while turn:
            player_defend_pos = input('Where would you like to defend the attack? High, middle or low. ').lower()
            possible_positions = ['high', 'middle', 'low']
            attack_pos = random.choice(possible_positions)
            while player_defend_pos not in [possible_positions] and player_defend_pos not in possible_actions:
                player_defend_pos = input('Where would you like to defend the attack? High, middle or low. ').lower()
            if player_defend_pos == 'm':
                use_medkit()
            elif player_defend_pos == 'help':
                help()
            elif player_defend_pos == 's':
                shop()
            elif player_defend_pos != attack_pos:
                if equipped_armour == False:
                    print(f'You don\'t have any armour equipped so the {self.name} will do {self.damage} damage to you.')
                    player['health'] -= self.damage
                    if attack_pos == 'high':
                        print(f'The {self.name} attacked you up high')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                    elif attack_pos == 'middle':
                        print(f'The {self.name} attacked you in the middle')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                    else:
                        print(f'The {self.name} attacked you down low')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                elif equipped_armour == True and shop_items[1][player['armours'][0]   ' protection'] > self.damage:
                    print(f'Since you have {[player["armours"][0]]} equipped it will not do anything.')
                    if attack_pos == 'high':
                        print(f'The {self.name} attacked you up high')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                    elif attack_pos == 'middle':
                        print(f'The {self.name} attacked you in the middle')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                    else:
                        print(f'The {self.name} attacked you down low')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                elif equipped_armour == True and shop_items[1][player['armours'][0]   ' protection'] < self.damage:
                    player['health'] -= self.damage - shop_items[1][player['armours'][0]   ' protection']
                    if attack_pos == 'high':
                        print(f'The {self.name} attacked you up high')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                    elif attack_pos == 'middle':
                        print(f'The {self.name} attacked you in the middle')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
                    else:
                        print(f'The {self.name} attacked you down low')
                        print(f'Your health: {player["health"]}')
                        print(f'{self.name}\'s health: {self.health}')
                        turn = False
                        break
            else:
                print('You have successfully defended the attack')
                turn = False
                break

This is the defend code there is a similar thing in attacking the NPC also which has the same output. Now even if I enter 'high', 'middle' or 'low' It asks the question again and again. Please show me some guidance.

CodePudding user response:

I think your problem is in

# this will produce always true
 while player_defend_pos not in [possible_positions]

it should be

while player_defend_pos not in possible_positions

I hope my answer help your problem

CodePudding user response:

This is not reproducible code - however I would recommend putting debug statements around player_defend_pos. Put something like print(player_defend_pos)

  • Related