'How to change the set value everytime the "while" loop resets
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?
Solution 1:[1]
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.
Solution 2:[2]
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 notes:
You should consider that
player_hpandenemy_hpshould be checked continuously! so they should be written in thewhileloop, not the outside!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_hpby these numbers! soenemy_attackis useless in this code!Conditions for
whileloop should be changed toplayer_hp >0 and enemy_hp >0!player_hp >0means that player is alive. this is meaningful forenemy_hp >0as well!as mwo said:
You should not rely on sets to provide random arrangements of values.
So we can use the
randomlibrary to randomly choose a value fromdice_listwithrandom.choice(dice_list).It's not a good idea to define
dice_rollinside thewhileloop. in this way, you are definingdice_rollagain and again where this isn't necessary at all! so it's recommended to define it before thewhileloop. in this case, you can definedice_listlike this beforewhileloop:dice_list=["1","2","3","4","5"]Also you don't need to define many conditions for subtracting the damage from
player_hp!player_hp= player_hp-valuewould be enough and you don't need toif/elifstatements there.
Solution 3:[3]
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
Solution 4:[4]
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}')
Solution 5:[5]
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")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | FLAK-ZOSO |
| Solution 4 | |
| Solution 5 | Vale |
