Home > Back-end >  How to change the set value everytime the "while" loop resets
How to change the set value everytime the "while" loop resets

Time:03-04

So I'm making a very simple battle mechanic in python where the player will be able to attack, defend or inspect the enemy :

print("------Welcome To The Game------")
player_hp=5
player_attack=3
enemy_hp=10
enemy_attack=2

while player_hp !=0 and enemy_hp !=0:
      Choice=input("""What will you do: 
      A.Attack
      B.Defend
      C.Inspect
      (A/B/C): """)
      if Choice=="A":
         enemy_hp-player_attack
         print("You dealt",str(player_attack), " damage"
      if Choice=="B":
         dice_roll=set(("1","2","3","4","5"))
         dice_list=list(dice_roll)
         value=dice_list[0]
         if value =="1":
            player_hp-1
         elif value=="2":
            player_hp-2
         elif value=="3":
            player_hp-3
         elif value=="4":
            player_hp-4
         elif value=="5":
            player_hp-5
       if Choice=="C":
          print("""Enemy hp is,enemy_hp
                Enemy attack is ,enemy_attack""")
else:
   if player_hp ==0:
      print("you lost")
   if enemy_hp==0:
      print("you won")

Problem that I'm having is that the value number doesn't reset after the loop finishes , if let's say the value first was 2 damage it will remain 2 everytime you press B until your hp finishes , so how can I make it that everytime you press the defend option the value is different?

CodePudding user response:

You should not rely on sets to provide random arrangements of values. In this case you should use random.randint function. Example:

import random

if Choice== "B":
    player_hp -= random.randint(1, 5)

Also as Shayan pointed out you are not modifying player_hp by doing player_hp - ... you should use player_hp -= ... instead.

CodePudding user response:

The problem is where you don't change the enemy_hp and player_hp! For example, when player choose to attack, then enemy_hp should decrease by enemy_hp = enemy_hp-player_attack.
This is also necessary for player_hp too!

So I think the code will be:

import random
print("------Welcome To The Game------")
player_hp=5
player_attack=3
enemy_hp=10
enemy_attack=2

while player_hp >0 and enemy_hp >0:
    Choice=input("""What will you do: 
    A.Attack
    B.Defend
    C.Inspect
    (A/B/C): """)
    if Choice=="A":
        enemy_hp =enemy_hp-player_attack
        print("You dealt",str(player_attack), f" damage and enemy hp is {enemy_hp}")
        
    elif Choice=="B":
        dice_roll=set(("1","2","3","4","5"))
        dice_list=list(dice_roll)
        value= random.choice(dice_list)
        if value =="1":
            player_hp= player_hp-1
        elif value=="2":
            player_hp=player_hp-2
        elif value=="3":
            player_hp=player_hp-3
        elif value=="4":
            player_hp=player_hp-4
        elif value=="5":
            player_hp=player_hp-5
        

    elif Choice=="C":
        print(f"Enemy hp is,{enemy_hp} Enemy attack is {value}")

if enemy_hp<=0:
    print("you won")

elif player_hp <=0:
    print("you lost")

Important note:

  1. You should consider that player_hp and enemy_hp should be checked continuously! so they should be written in the while loop, not the outside!

  2. Another Important point is based on your code this can be concluded that the damage of the enemy is randomly chosen between numbers from 1 to 5! Because you decreased player_hp by these numbers! so enemy_attack is useless in this code!

  3. Conditions for while loop should be changed to player_hp >0 and enemy_hp >0! player_hp >0 means that player is alive. this is meaningful for enemy_hp >0 as well!

  4. as mwo said:

    You should not rely on sets to provide random arrangements of values.

    So we can use the random library to randomly choose a value from dice_list with random.choice(dice_list).

CodePudding user response:

     dice_roll=set(("1","2","3","4","5"))
     dice_list=list(dice_roll)
     value=dice_list[0]
     ...
        player_hp-4
     elif value=="5":
        player_hp-5

This code you created is a way of generating a random number, but there are better ways of doing this.


Using the module random, you can access the function random.randint, which generates a random int between two given numbers:

>>> random.randint(0, 10) # It includes both the extremes
4

So a better idea would be:

player_hp-=random.randint(1, 5)

You should also be aware that:

player_hp-5

doesn't reduce player_hp by 5, because it's not an assignation, you can do it in one of this ways:

player_hp-=5
# ...or...
player_hp = player_hp-5

CodePudding user response:

how can I make it that everytime you press the defend option the value is different

You should look at using the random module

Ignoring the game logic, here's a simple example

import random

dice_values = list(range(1, 7))  # example for six-sided die

alive = True
while alive:
  value = random.choice(dice_values)
  print(f'you picked {value}')

CodePudding user response:

As the others have already recommended, I would also use the random function for an randomly chosen int between 1 and 5. If I understood your program correctly, in choice B you want the chosen value to be the players defence and the attack from the opponent to be subtracted from it. This would be my suggested solution

import random
print("------Welcome To The Game------")
min = 1
max = 5
player_hp=5
player_attack=3
enemy_hp=10
enemy_attack=

while player_hp>0 and enemy_hp>0:
      Choice=input("""What will you do: 
      A.Attack
      B.Defend
      C.Inspect
      (A/B/C): """)
      if Choice=="A":
         enemy_hp = enemy_hp-player_attack
         print("You dealt",str(player_attack), "damage")
      elif Choice=="B":
         defence = random.randint(min,max)
         print("value of defense is",defence)
         damage = defence - enemy_attack
         if damage < 0:
            print("value of damage is",damage)
            player_hp = player_hp - (-damage)
         else: 
            print("there was no damage")
      elif Choice=="C":
         print("player hp is",player_hp)
         print("enemy hp is",enemy_hp)
else:
    if player_hp<=0:
        print("you lost")
    if enemy_hp<=0:
        print("you won")
  • Related