So, here is what I'd like to do.
Here is my code:
def weapon():
'''A list of the available weapon'''
print("Here is your weapon choices for today: ")
print("1. Flail:\n Damage: 1,6 \n Heal: 1, 3")
print("2. Dagger:\n Damage: 2, 4 \n Heal: 2, 3")
print("3. Sword:\n Damage: 1, 7 \n Heal: 1, 2")
print("4. Crossbow:\n Damage: 0, 10 \n Heal: 0, 0")
print("5. Mace:\n Damage: 1, 8 \n Heal: 0, 0")
print("6. Quarterstaff:\n Damage 3,10 \n Heal 0,5")
# Covert to integer to select weapon:
WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
"\n 3: Sword,"
"\n 4: Crossbow, \n 5: Mace,"
"\n 6: Quarterstaff \n"
"Press the corresponding number for your weapon:\n "))
I want to change the function to a dictionary or maybe a class so that this string format can work:
print('You choice a,'
'{WEAPON} with {DAMAGE} and'
'{HEAL} capabilties'.format(WEAPON=weapon, DAMAGE=Damage, HEAL=Heal))
I'd also add a confirmation message and allow the user to choose another weapon.
Here is the full code
from random import randint
NAMES = ["Ryu", "Bruce Lee", "Jet Li", "Steven Segal", "Batman",
"The Flash", "Chuck Norris", "Frieza",
"Cell", "Wonder Woman", "Goku", "Vegeta"]
NAMEINT = randint(0, 11)
if NAMEINT == 0:
FIGHTERNAME = NAMES[0]
elif NAMEINT == 1:
FIGHTERNAME = NAMES[1]
elif NAMEINT == 2:
FIGHTERNAME = NAMES[2]
elif NAMEINT == 3:
FIGHTERNAME = NAMES[3]
elif NAMEINT == 4:
FIGHTERNAME = NAMES[4]
elif NAMEINT == 5:
FIGHTERNAME = NAMES[5]
elif NAMEINT == 6:
FIGHTERNAME = NAMES[6]
elif NAMEINT == 7:
FIGHTERNAME = NAMES[7]
elif NAMEINT == 8:
FIGHTERNAME = NAMES[8]
elif NAMEINT == 9:
FIGHTERNAME = NAMES[9]
elif NAMEINT == 10:
FIGHTERNAME = NAMES[10]
elif NAMEINT == 11:
FIGHTERNAME = NAMES[11]
# Intro Text
NAME = input("Hello what is your name?\n")
print("Hello", NAME, "Today you will be fighter", FIGHTERNAME)
# Starting Health
PLAYERHP = 10
ENEMYHP = 10
# Weapon Inventory
def weapon():
'''A list of the available weapon'''
print("Here is your weapon choices for today: ")
print("1. Flail:\n Damage: 1,6 \n Heal: 1, 3")
print("2. Dagger:\n Damage: 2, 4 \n Heal: 2, 3")
print("3. Sword:\n Damage: 1, 7 \n Heal: 1, 2")
print("4. Crossbow:\n Damage: 0, 10 \n Heal: 0, 0")
print("5. Mace:\n Damage: 1, 8 \n Heal: 0, 0")
print("6. Quarterstaff:\n Damage 3,10 \n Heal 0,5")
# Covert to integer to select weapon
WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
"\n 3: Sword,"
"\n 4: Crossbow, \n 5: Mace,"
"\n 6: Quarterstaff \n"
"Press the corresponding number for your weapon:\n"))
# Confirming Weapon Choice
print('You choice a,'
'{WEAPON} with {DAMAGE} and'
'{HEAL} capabilties'.format(WEAPON=weapon, DAMAGE=Damage, HEAL=Heal))
def confirm(prompt=None, resp=False):
"""prompts for yes or no response from the user. Returns True for yes and
False for no.
"""
if prompt is None:
prompt = 'Confirm Weapon Choice'
if resp:
prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
else:
prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
while True:
ans = input(prompt)
if not ans:
return resp
if ans not in ['y', 'Y', 'n', 'N']:
print ('please enter y or n.')
continue
if ans == 'y' or ans == 'Y' or ans == 'Yes':
return True
if ans == 'n' or ans == 'N' or ans == 'No':
return False
# Combat System Loop with weapons
while True:
if WEAPON == 1:
DAMAGE = randint(1, 6)
HEAL = randint(1, 3)
elif WEAPON == 2:
DAMAGE = randint(2, 4)
HEAL = randint(2, 3)
elif WEAPON == 3:
DAMAGE = randint(1, 7)
HEAL = randint(1, 2)
elif WEAPON == 4:
DAMAGE = randint(0, 9)
HEAL = randint(0, 0)
elif WEAPON == 5:
DAMAGE = randint(0, 9)
HEAL = randint(0, 0)
elif WEAPON == 6:
DAMAGE = randint(3, 10)
HEAL = randint(0, 5)
NEWDAMAGE = randint(0, 6)
print("the enemies hp is:", ENEMYHP)
print("your hp is:", PLAYERHP)
CHOICE = input("Would you like to attack or heal (ATT/HEA)")
if CHOICE == "ATT":
ENEMYHP = ENEMYHP - DAMAGE
PLAYERHP = PLAYERHP - NEWDAMAGE
if ENEMYHP <= 0:
print("Well done you defeated", FIGHTERNAME)
break
elif PLAYERHP <= 0:
print("You were defeated by", FIGHTERNAME)
break
if CHOICE == "HEA":
PLAYERHP = PLAYERHP + HEAL
PLAYERHP = PLAYERHP - NEWDAMAGE
if ENEMYHP <= 0:
print("Well done you defeated", FIGHTERNAME)
break
elif PLAYERHP <= 0:
print("You were defeated by", FIGHTERNAME)
break
ENDGAME = input("Press Enter to end the game")
EDIT 2 HERE is my updated code
from random import randint
NAMES = ["Ryu", "Bruce Lee", "Jet Li", "Steven Segal", "Batman",
"The Flash", "Chuck Norris", "Frieza",
"Cell", "Wonder Woman", "Goku", "Vegeta"]
NAMEINT = randint(0, 11)
if NAMEINT == 0:
FIGHTERNAME = NAMES[0]
elif NAMEINT == 1:
FIGHTERNAME = NAMES[1]
elif NAMEINT == 2:
FIGHTERNAME = NAMES[2]
elif NAMEINT == 3:
FIGHTERNAME = NAMES[3]
elif NAMEINT == 4:
FIGHTERNAME = NAMES[4]
elif NAMEINT == 5:
FIGHTERNAME = NAMES[5]
elif NAMEINT == 6:
FIGHTERNAME = NAMES[6]
elif NAMEINT == 7:
FIGHTERNAME = NAMES[7]
elif NAMEINT == 8:
FIGHTERNAME = NAMES[8]
elif NAMEINT == 9:
FIGHTERNAME = NAMES[9]
elif NAMEINT == 10:
FIGHTERNAME = NAMES[10]
elif NAMEINT == 11:
FIGHTERNAME = NAMES[11]
# Intro Text
MYNAME = input("Hello what is your name?\n")
print("Hello", MYNAME, "Today you will be fighter", FIGHTERNAME)
# Starting Health
PLAYERHP = 10
ENEMYHP = 10
# Weapon Inventory
def weapon():
'''A list of the available weapon'''
print("Here is your weapon choices for today: ")
print("1. Flail:\n Damage: 1,6 \n Heal: 1, 3")
print("2. Dagger:\n Damage: 2, 4 \n Heal: 2, 3")
print("3. Sword:\n Damage: 1, 7 \n Heal: 1, 2")
print("4. Crossbow:\n Damage: 0, 10 \n Heal: 0, 0")
print("5. Mace:\n Damage: 1, 8 \n Heal: 0, 0")
print("6. Quarterstaff:\n Damage 3,10 \n Heal 0,5")
# Covert to integer to select weapon
WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
"\n 3: Sword,"
"\n 4: Crossbow, \n 5: Mace,"
"\n 6: Quarterstaff \n"
"Press the corresponding number for your weapon:\n"))
if WEAPON == 1:
NAME = Flail
DAMAGE = randint(1, 6)
HEAL = randint(1, 3)
elif WEAPON == 2:
NAME = Dagger
DAMAGE = randint(2, 4)
HEAL = randint(2, 3)
elif WEAPON == 3:
NAME = Sword
DAMAGE = randint(1, 7)
HEAL = randint(1, 2)
elif WEAPON == 4:
NAME = Crossbow
DAMAGE = randint(0, 9)
HEAL = randint(0, 0)
elif WEAPON == 5:
NAME = Mace
DAMAGE = randint(0, 9)
HEAL = randint(0, 0)
elif WEAPON == 6:
NAME = Quarterstaff
DAMAGE = randint(3, 10)
HEAL = randint(0, 5)
# Confirming Weapon Choice
print('You choice a,'
'{NAME} with {DAMAGE} and'
'{HEAL} capabilties'.format(NAME=NAME, DAMAGE=DAMAGE, HEAL=HEAL))
def confirm(prompt=None, resp=False):
"""Prompts for yes or no response from the user."""
if prompt is None:
prompt = 'Confirm Weapon Choice'
if resp:
prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
else:
prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
while True:
ans = input(prompt)
if not ans:
return resp
if ans not in ['y' 'Y', 'n', 'N']:
print('please enter y or n.')
continue
if ans in ['y', 'Y', 'Yes']:
return True
if ans in ['n', 'N', 'No']:
return False
# Combat System Loop with weapons
while True:
if WEAPON == 1:
DAMAGE = randint(1, 6)
HEAL = randint(1, 3)
elif WEAPON == 2:
DAMAGE = randint(2, 4)
HEAL = randint(2, 3)
elif WEAPON == 3:
DAMAGE = randint(1, 7)
HEAL = randint(1, 2)
elif WEAPON == 4:
DAMAGE = randint(0, 9)
HEAL = randint(0, 0)
elif WEAPON == 5:
DAMAGE = randint(0, 9)
HEAL = randint(0, 0)
elif WEAPON == 6:
DAMAGE = randint(3, 10)
HEAL = randint(0, 5)
NEWDAMAGE = randint(0, 6)
print("the enemies hp is:", ENEMYHP)
print("your hp is:", PLAYERHP)
CHOICE = input("Would you like to attack or heal (ATT/HEA)")
if CHOICE == "ATT":
ENEMYHP = ENEMYHP - DAMAGE
PLAYERHP = PLAYERHP - NEWDAMAGE
if ENEMYHP <= 0:
print("Well done you defeated", FIGHTERNAME)
break
elif PLAYERHP <= 0:
print("You were defeated by", FIGHTERNAME)
break
if CHOICE == "HEA":
PLAYERHP = PLAYERHP + HEAL
PLAYERHP = PLAYERHP - NEWDAMAGE
if ENEMYHP <= 0:
print("Well done you defeated", FIGHTERNAME)
break
elif PLAYERHP <= 0:
print("You were defeated by", FIGHTERNAME)
break
ENDGAME = input("Press Enter to end the game")
Now, it's stating that Name isn't defined so I'm thinking of changing
WEAPON = int(input("Choose your weapon: \n 1: Flail \n 2: Dagger,"
"\n 3: Sword,"
"\n 4: Crossbow, \n 5: Mace,"
"\n 6: Quarterstaff \n"
"Press the corresponding number for your weapon:\n"))
into a dictionary with the key being the weapon name and the values being damage and heal. Is there a way to do this. I.E. have a dictionary with 1 key that has 2 values generated randomly?
PS. I've used pycodestyle and pylint for formatting and PEP8 checks.
UPDATE.
It mostly works now. I was able to complete a loop of the game, however, the confirmation loop doesn't work. What is suppose to happen is that you have the warning to confirm your weapon but if you don't like it you can go back to the weapon selection.
def confirm(prompt=None, resp=False):
"""Prompts for yes or no response from the user."""
if prompt is None:
prompt = 'Confirm Weapon Choice'
if resp:
prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
else:
prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
while True:
ans = input(prompt)
if not ans:
return resp
if ans not in ['y' 'Y', 'n', 'N']:
print('please enter y or n.')
continue
if ans in ['y', 'Y', 'Yes']:
return True
if ans in ['n', 'N', 'No']:
return False
confirm()
Now, I'm sure I missed something like how to loop it back to the previous selection, just not sure how to do that.
The reason your string format isn't working and you get the error is because you're defining the NAME
variable with undefined variables. Wrap all your weapon names in speech marks to make them strings.
if WEAPON == 1:
NAME = 'Flail' #Change this to a string
DAMAGE = randint(1, 6)
HEAL = randint(1, 3)
elif WEAPON == 2:
NAME = 'Dagger' #and this
DAMAGE = randint(2, 4)
HEAL = randint(2, 3)
elif WEAPON == 3:
NAME = 'Sword' #and this
DAMAGE = randint(1, 7)
HEAL = randint(1, 2)
elif WEAPON == 4:
NAME = 'Crossbow' #and this etc...
DAMAGE = randint(0, 9)
HEAL = randint(0, 0)
You can clean up a few things here, but here's one suggestion.. change this;
if NAMEINT == 0:
FIGHTERNAME = NAMES[0]
elif NAMEINT == 1:
FIGHTERNAME = NAMES[1]
elif NAMEINT == 2:
FIGHTERNAME = NAMES[2]
elif NAMEINT == 3:
FIGHTERNAME = NAMES[3]
elif NAMEINT == 4:
FIGHTERNAME = NAMES[4]
elif NAMEINT == 5:
FIGHTERNAME = NAMES[5]
elif NAMEINT == 6:
FIGHTERNAME = NAMES[6]
elif NAMEINT == 7:
FIGHTERNAME = NAMES[7]
elif NAMEINT == 8:
FIGHTERNAME = NAMES[8]
elif NAMEINT == 9:
FIGHTERNAME = NAMES[9]
elif NAMEINT == 10:
FIGHTERNAME = NAMES[10]
elif NAMEINT == 11:
FIGHTERNAME = NAMES[11]
to this;
FIGHTERNAME = NAMES[NAMEINT]
It'll do the same job. You should also double check PEP8, as it should be more like fighter_name = names[nameint]
.