Reputation: 265
import random
hp = 100
eh = 100
x = 0
y = 0
print("Hello welcome to the beta version of my game.
This game is a 1 v 1 fight to the death against an unknown enemy.
In this game you and the enemy both start out with 100 health.
You can choose to either attack or heal each turn. Have fun and
pay attention to the following rules.")
print("Rule 1: You cannot heal while your health is over 84 points.
If you break this rule your turn will be void.")
print("Rule 2: You can only enter attack, Attack, heal, or Heal.
If you break this rule your turn will be void.")
print("Please press enter to start")
while hp > 0 and eh > 0:
print("Action? (attack, heal, nothing):")
act = input(">")
attack = random.randint(1, 30)
heal = random.randint(1, 15)
enemy_attack = random.randint(1, 30)
enemy_heal = random.randint(1, 15)
enemy_heal_within_5 = random.randint(1, 5)
enemy_decision = random.randint(1, 2)
if act == "attack" or act == "Attack":
eh = eh - attack
print(attack)
print("You have dealt %s damage" % attack)
elif act == "heal" and hp < 85:
hp = hp + heal
print("You have healed %s points" % heal)
elif act == "heal" and hp > 84:
while x == 0:
if act == "attack":
x +=1
else:
print("You cannot heal at this time, please try again.")
act = input(">")
if enemy_decision == 1:
hp = hp - enemy_attack
print("The enemy has dealt %s damage" % enemy_attack)
elif enemy_decision == 2 and eh > 94:
pass
elif enemy_decision == 2:
eh = eh + enemy_heal_within_5
print("The enemy has healed %s points" % enemy_heal_within_5)
elif enemy_decision == 2 and eh < 85:
eh = eh + enemy_heal
print("The enemy has healed %s points" % enemy_heal)
else:
print("Your health is now %s" % hp)
print("The enemy's health is now %s" % eh)
if hp <= 0:
print("You have lost")
elif eh <= 0:
print("You have won")
I need help creating an else statement where if the player enters something other than attack or heal, it asks them to try to input either attack or heal again. I tried repeating what I did in the hp > 84 section, but it ended up running that section instead of the else section.
Upvotes: 0
Views: 163
Reputation: 2323
You can make something like:
...
while hp > 0 and eh > 0:
act = "empty"
print("Action? (attack, heal, nothing):")
# With this while, you are accepting anything like "aTTaCk", "AttaCK", etc
while act.lower() not in ["attack","heal", "", "nothing"]:
act = input(">")
attack = random.randint(1, 30)
heal = random.randint(1, 15)
enemy_attack = random.randint(1, 30)
enemy_heal = random.randint(1, 15)
enemy_heal_within_5 = random.randint(1, 5)
enemy_decision = random.randint(1, 2)
...
I added the option nothing
and also an empty string(""
) as an option if the player doesn't want to make anything. If you don't need any of them, just delete both from the list in while
statement.
Upvotes: 1
Reputation: 19264
import random
hp = 100
eh = 100
x = 0
y = 0
print("Hello welcome to the beta version of my game. This game is a 1 v 1 fight to the death against an unknown enemy. In this game you and the enemy both start out with 100 health. You can choose to either attack or heal each turn. Have fun and pay attention to the following rules.")
print("Rule 1: You cannot heal while your health is over 84 points. If you break this rule your turn will be void.")
print("Rule 2: You can only enter attack, Attack, heal, or Heal. If you break this rule your turn will be void.")
print("Please press enter to start")
while hp > 0 and eh > 0:
act = ""
print("Action? (attack, heal, nothing):")
while act.lower() != "attack" and act.lower() != "heal":
act = input(">")
attack = random.randint(1, 30)
heal = random.randint(1, 15)
enemy_attack = random.randint(1, 30)
enemy_heal = random.randint(1, 15)
enemy_heal_within_5 = random.randint(1, 5)
enemy_decision = random.randint(1, 2)
if act == "attack" or act == "Attack":
eh = eh - attack
print(attack)
print("You have dealt %s damage" % attack)
elif act == "heal" and hp < 85:
hp = hp + heal
print("You have healed %s points" % heal)
elif act == "heal" and hp > 84:
while x == 0:
if act == "attack":
x +=1
else:
print("You cannot heal at this time, please try again.")
act = input(">")
if enemy_decision == 1:
hp = hp - enemy_attack
print("The enemy has dealt %s damage" % enemy_attack)
elif enemy_decision == 2 and eh > 94:
pass
elif enemy_decision == 2:
eh = eh + enemy_heal_within_5
print("The enemy has healed %s points" % enemy_heal_within_5)
elif enemy_decision == 2 and eh < 85:
eh = eh + enemy_heal
print("The enemy has healed %s points" % enemy_heal)
else:
print("Your health is now %s" % hp)
print("The enemy's health is now %s" % eh)
if hp <= 0:
print("You have lost")
elif eh <= 0:
print("You have won")
Use a while
loop that checks whether the input is not "attack"
and if it is not "heal"
, or any capitalized version of the two. I use !=
, but you can also use not
, as Ruben Bermudez showed below.
Upvotes: 0