wintermute2-0
wintermute2-0

Reputation: 9

Python - getting errors in a text-based fighting game

I'm very new to Python and programming in general. I'm trying to make a simple text-based fighting game for my kids to play. It's the first program I've written on my own. I keep getting errors, and I'm not sure what it is I'm doing wrong.

Here's the code:

import random
print("Welcome to Sword Fight!")

#Character Stat Section
#Strength
x_strength = 8
y_strength = 7
z_strength = 4
a_strength = 2
#Speed
x_spd = 4
y_spd = 6
z_spd = 4
a_spd = 3
#Agility
x_agl = 3
y_agl = 5
z_agl = 6
a_agl = 2

#Weapon Stat Section
#Strength
katana_strength = 5
broadsword_strength = 6
spear_strength = 7
dagger_strength = 3
#Speed
katana_spd = 5
broadsword_spd = 4
spear_spd = 3
dagger_spd = 7
#Reach
katana_rch = 5
broadsword_rch = 6
spear_rch = 7
dagger_rch = 3


#Enemy Character Section
enemy = ['a goblin', 'a ninja', 'a swordfighter', 'an orc']
#Strength
goblin_strength = 2
ninja_strength = 4
swordfighter_strength = 5
orc_strength = 7
#Speed
goblin_spd = 4
ninja_spd = 7
swordfighter_spd = 5
orc_spd = 3
#Agility
goblin_agl = 3
ninja_agl = 6
swordfighter_agl = 5
orc_agl = 2

#Enemy Weapon Stat Section
#Strength
shiv_strength = 2
ninjato_strength = 4
longsword_strength = 5
axe_strength = 7
#Speed
shiv_spd = 6
ninjato_spd = 6
longsword_spd = 5
axe_spd = 3
#Reach
shiv_rch = 2
ninjato_rch = 4
longsword_rch = 5
axe_rch = 4

player = str(input("Select your character - X, Y, Z or A: "))
if player == "X" or "x":
    print("You've selected X.")
    p_strength = x_strength
    p_spd = x_spd
    p_agl = x_agl
elif player == "Y" or "y":
    print("You've selected Y.")
    p_strength = y_strength
    p_spd = y_spd
    p_agl = y_agl
elif player == "Z" or "z":
    print("You've selected Z.")
    p_strength = z_strength
    p_spd = z_spd
    p_agl = z_agl
elif player == "A" or "a":
    print("You've selected A.")
    p_strength = a_strength
    p_spd = a_spd
    p_agl = a_agl

weapon = str(input("Select your weapon - katana, broadsword, spear, or dagger: "))
if weapon == "Katana" or "katana":
    p_weap = 'katana'
    print("You've selected a katana.")
    w_strength = katana_strength
    w_spd = katana_spd
    w_rch = katana_rch
elif weapon == "Broadsword" or "broadsword":
    p_weap = 'broadsword'
    print("You've selected a broadsword.")
    w_strength = broadsword_strength
    w_spd = broadsword_spd
    w_rch = broadsword_rch
elif weapon == "Spear" or "spear":
    p_weap = 'spear'
    print("You've selected a spear.")
    w_strength = spear_strength
    w_spd = spear_spd
    w_rch = spear_rch
elif weapon == "Dagger" or "dagger":
    p_weap = 'dagger'
    print("You've selected a dagger.")
    w_strength = dagger_strength
    w_spd = dagger_spd
    w_rch = dagger_rch

print("An enemy approaches! It's", random.choice(enemy), "!")

if random.choice(enemy) == 'a goblin':
    print("It's armed with a shiv.")
    e_strength = goblin_strength
    e_spd = goblin_spd
    e_agl = goblin_agl
    ew_strength = shiv_strength
    ew_spd = shiv_spd
    ew_rch = shiv_rch
elif random.choice(enemy) == 'a ninja':
    print("He's armed with a ninjato.")
    e_strength = ninja_strength
    e_spd = ninja_spd
    e_agl = ninja_agl
    ew_strength = ninjato_strength
    ew_spd = ninjato_spd
    ew_rch = ninjato_rch
elif random.choice(enemy) == 'a swordfighter':
    print("She's armed with a longsword.")
    e_strength = swordfighter_strength
    e_spd = swordfighter_spd
    e_agl = swordfighter_agl
    ew_strength = longsword_strength
    ew_spd = longsword_spd
    ew_rch = longsword_rch
elif random.choice(enemy) == 'an orc':
    print("It's armed with a battleaxe.")
    e_strength = orc_strength
    e_spd = orc_spd
    e_agl = orc_agl
    ew_strength = axe_strength
    ew_spd = axe_spd
    ew_rch = axe_rch

print("The battle begins!")

p_hp = 10
e_hp = 10

while p_hp > 0 or e_hp > 0:
    print = ("Which attack will you use?")
    if p_weap == 'katana':
        attack = str(input("Slice, Stab, or Spinning Slash?: "))
    if attack == 'Slice' or 'slice':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Stab' or 'stab':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Spinning slash' or 'spinning slash' or 'Spinning Slash':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")    
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if p_weap == 'broadsword':
        attack = str(input("Overhead chop, Stab, or Slash?: "))
    if attack == 'Overhead chop' or 'overhead chop' or 'Overhead Chop':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Stab' or 'stab':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Slash' or 'slash':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if p_weap == 'spear':
        attack = str(input("Slam, Swipe, or Thrust?: "))
    if attack == 'Slam' or 'slam':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Swipe' or 'swipe':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Thrust' or 'thrust':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if p_weap == 'dagger':
        attack = str(input("Slash, Stab, or Thrust?: "))
    if attack == 'Slash' or 'slash':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Stab' or 'stab':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Thrust' or 'thrust':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

if p_hp == 0:
    print("You have been defeated...")
if e_hp == 0:
    print("You are victorious!")

I'm sure there are simpler ways I could be doing all of this, but I haven't yet learned more advanced commands or techniques.

The first error I'm running into is that no matter what the user enters for their character selection, the program only picks character X.

Second error - regardless of what weapon the user selects, the program picks the katana.

Third error - when the program selects an enemy, sometimes it selects the correct enemy weapon, sometimes the wrong enemy weapon, and sometimes it doesn't select an enemy weapon at all.

Fourth error - If the program selects an enemy and the correct enemy weapon, I get a traceback error when the user enters an attack:

line 171, in <module>
    print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
TypeError: 'str' object is not callable
1|:/ $

Fifth error - When the program fails to select an enemy weapon, and the user enters an attack, I get a different traceback error:

line 168, in <module>
    e_atk = (e_strength + ew_strength)
NameError: name 'e_strength' is not defined
1|:/ $

Sixth error - when the program selects an enemy and the incorrect enemy weapon, and the user enters an attack, another traceback error:

line 174, in <module>
    print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
TypeError: 'str' object is not callable
1|:/ $

Any help would be appreciated. Thanks!

Upvotes: 0

Views: 181

Answers (2)

James
James

Reputation: 56

  1. The first problem is that your or statement is wrong. if player == "X" is correct, however or player "x": will always return true as you are essentially asking "does the following string 'x' exist?" if you are going to keep doing it this way you need to change all your if or statements to if player == "X" or player == "x":

    • now an easier way of doing this would be to change all your inputs (I'll use select character as an example)

    from: player = str(input("Select your character - X, Y, Z or A: "))

    to: player = str(input("Select your character - X, Y, Z or A:")).lower()

    the neat thing about this is now whatever the player inputs will be lowercase. All you need to do is check if it is equal to a lowercase string. If the player types "HeLlOw WoRlD" lower() will convert it to "hello world".

  2. The same as problem one you need to change all the if statements like this if weapon == "Katana" or "katana": to if weapon == "Katana" or weapon == "katana":

    • again if you make the input lowercase you only would have to check if weapon == "katana".
  3. e_strength is defined in several if statements but never outside any, you need to put it in the outer scope. see this explanation of scope in python for more info. basically the answer is that you need to first define e_strength outside the if statements, maybe on line 123 and give it any value you want as the idea is that it should be overwritten. This seems to be a reoccurring issue, after fixing it a few other scope related bugs become apparent.

  4. Wow this was a funky one. line 163 you wrote print = ("Which attack will you use?") this should be print("Which attack will you use?") this completely broke all the code after it.

  5. same problem from 4.

  6. same problem from 4.

Upvotes: 1

Dain Im
Dain Im

Reputation: 11

the third issue is that every time you run random.choice(enemy), it randomly picks a new enemy every time. You should put random.choice(enemy) into a variable and then run through the if/else statements.

as for the fourth issue, I would suggest using .format instead.

before

print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")

after

print("You slice the enemy and do {} points of damage.".format(p_atk - e_atk))

The fifth and sixth errors should be fixed when putting random.choice(enemy) into a variable.

Upvotes: 1

Related Questions