Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can someone help me figure out this syntax error

+4 votes
asked Mar 30 by Ryder Johansen (220 points) 1 flag
I'm making a text adventure game and I can't figure out this syntax error. Heres the game https://onlinegdb.com/rJTjiWgBO.

And Heres the error it gives me

  File "main.py", line 80
    if Action == "Basic Bow":
                            ^
SyntaxError: invalid syntax

3 Answers

+2 votes
answered Mar 30 by Peter Minarik (22,320 points)
selected Mar 31 by Ryder Johansen
 
Best answer

Code Cleanup

I've cleaned up your code a bit, created classes and functions to reduce the copy-pasted code and provide a much clearer functionality.

import random
import time

def GetChoice(message, choices):
    success = False
    while (not success):
        choice = input(message + " " + str(choices) + ": ").strip().upper()
        success = choice in choices
    return choice

    
class Player:
    name = "yourself"
    maxHp = 50
    hp = 50
    inventory = "empty"

class Weapon:
    def CanHit(self):
        return random.randint(0, 100) < self.hitChance

    def TryHit(self, enemy):
        if self.CanHit():
            self.HitEnemy(enemy)
            enemy.TakeDamage(self.damage)
        else:
            print("You missed!")

    
class Sword(Weapon):
    hitChance = 85
    damage = 10
    def HitEnemy(self, enemy):
        print("You slash through the", enemy.name, "dealing", self.damage, "damage!")

class Bow(Weapon):
    hitChance = 75
    damage = 8
    def HitEnemy(self, enemy):
        print("You plunge your arrow into the", enemy.name, "dealing", self.damage, "damage!")

class Magic:
    def CanCast(self):
        return random.randint(0, 100) < self.castChance

    def TryCast(self, target):
        if self.CanCast():
            self.CastOnTarget(target)
        else:
            print("You missed!")
    

class Fireball(Magic):
    castChance = 65
    damage = 15
    def CastOnTarget(self, enemy):
        print("Forming a ball of fire in your hand you hurl it at the", enemy.name, "dealing", self.damage, "damage!")
        enemy.TakeDamage(self.damage)

class Healing(Magic):
    castChance = 100
    healMin = 5
    healMax = 15
    def CastOnTarget(self, target):
        if target.maxHp > target.hp:
            heal = random.randint(5, 15)
            if heal > target.maxHp - target.hp:
                heal = target.maxHp - target.hp
            print("You have healed", target.name, "for", heal, "HP.")
        else:
            print("Already at maximum health.")

class Enemy:
    def __init__(self, name):
        self.name = name

    def TakeDamage(self, damage):
        self.hp = self.hp - damage

class Beatle(Enemy):
    hp = 20
    def __init__(self):
        Enemy.__init__(self, "Beatle")

class Goblin(Enemy):
    hp = 50
    def __init__(self):
        Enemy.__init__(self, "Goblin")

class Kobold(Enemy):
    hp = 100
    def __init__(self):
        Enemy.__init__(self, "Kobold")

print ("welcome to Ryder's house of monstrositys!!!")
time.sleep (2)
print ("can you escape?")
time.sleep (1)

difficulty = GetChoice("What difficulty would you like?", ["EASY", "NORMAL", "HARD"])
print ("... preparing dungeon ...")
time.sleep (2)
counter = 3
while counter > 0:
    print (".")
    time.sleep (0.5)
    counter = counter - 1

if difficulty == "EASY":
    enemy = Beatle()
elif difficulty == "NORMAL":
    enemy = Goblin()
elif difficulty == "HARD":
    enemy = Kobold()

print("First encounter: " + enemy.name + "!")
choice = GetChoice("What would you like to do?", ["RUN", "FIGHT"])
if choice == "RUN":
    print ("GAME OVER!") 
    exit ()
elif choice == ("FIGHT"):
  print ("Battle Begin!")

print ("Your Turn!")
player = Player()
choice = GetChoice("What would you like to do?", ["ATTACK", "MAGIC", "INVENTORY", "RETREAT"])
if choice == "ATTACK":
    choice = GetChoice("What weapon would you like to use?", ["SWORD", "BOW"])
    if choice == "SWORD":
        weapon = Sword()
    elif choice == "BOW":
        weapon = Bow()
    else:
        print("[Error] Unknown weapon: " + choice)
    weapon.TryHit(enemy)
elif choice == "MAGIC":
    choice = GetChoice("Which spell would you like to cast?", ["FIRE", "HEAL"])
    if choice == "FIRE":
        fireball = Fireball()
        fireball.TryCast(enemy)
    elif choice == "HEAL":
        healing = Healing()
        healing.TryCast(player)
commented Mar 30 by Ryder Johansen (220 points)
Thanks mate! I'm still kinda new to coding so I appreciate the help!
0 votes
answered Mar 30 by Peter Minarik (22,320 points)

You have lots of indentation errors and missing closing parentheses (')').

I've fixed these syntax errors for you:

maxhp = int(50)
hp = int(50)
import random
import time
print ("welcome to Ryder's house of monstrositys!!!")
time.sleep (2)
print ("can you escape?")
time.sleep (1)
difficulty = input ('what difficulty would you like, easy normal or hard?')
print ("... preparing dungeon ...")
time.sleep (2) 
print (".") 
time.sleep (0.5) 
print (".") 
time.sleep (0.5) 
print (".") 
time.sleep (0.5) 
if difficulty == " easy":
     print ('first encounter: Beatle!')
     encounter = "Beatle"
elif difficulty == "easy":
   print ('first encounter: Beatle!')
   encounter = "Beatle"
elif difficulty == " normal":
    print ("first encounter: Goblin!")
    encounter = "Goblin"
elif difficulty == "normal":
    print ("first encounter: Goblin!")
    encounter = "Goblin"
elif difficulty == " hard":
    print ("first encounter: Kobold ")
    encounter = "Kobold"
elif difficulty == "hard":
    print ("first encounter: Kobold ")
    encounter = "Kobold"
else:
    print ("ERROR INVALID DIFFICULTY BEEP BOOP BOOM")
    exit ()
if difficulty == "easy":
    enemyhp = int(20)
elif difficulty == ' easy':
    enemyhp = int(20)
elif difficulty == "normal":
    enemyhp = int(50)
elif difficulty == " normal":
    enemyhp = int(50)
elif difficulty == 'hard':
    enemyhp = int(100)
elif difficulty == " hard":
    enemyhp = int(100)
choice = input ("Run or Fight?")
if choice == " run":
    print ("GAME OVER!") 
    exit ()
elif choice == "run":
    print ("GAME OVER!") 
    exit ()
elif choice == (" fight"):
    print ("Battle Begin!") 
elif choice == ("fight"):
  print ("Battle Begin!") 
hit = int(7)
inventory = "empty"
print ("Your Turn!")
Action = input("what would you like to do: attack, magic, inventory, or retreat")
if Action == "attack":
    Action = input("here are your weapons: Basic sword, and Basic bow")
    if Action == "basic sword":
        Action = "Basic Sword"
    elif Action == "Basic sword":
        Action = "Basic Sword"
    elif Action == "Sword":
        Action = "Basic Sword"
    elif Action == "Sword":
        Action = "Basic Sword"
    if Action == "Basic Sword":
        hit = random.randint(1, 4)
        if hit > 1: 
            print ("you slash through the",encounter, "dealing 10 damage!")
    if Action == "Basic Bow":
        hit = random.randit(1, 4)
        if hit > 1:
            print ("you plunge your arrow into the", encounter, "dealing 10 damage")
elif Action == " attack":
    Action = input("here are your weapons: Basic sword, and Basic bow")
    if Action == "Basic Sword":
        hit = random.randint(1, 4)
        if hit > 1: 
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "Sword":
        hit = random.randint(1, 4)
        if hit > 1:
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "basic sword":
        hit = random.randint(1, 4)
        if hit > 1: 
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "sword":
        hit = random.randint(1, 4)
        if hit > 1: 
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "Basic Bow":
        hit = random.randit(1, 4)
        if hit > 1:
            print ("you plunge your arrow into the", encounter, "dealing 10 damage")
    elif Action == "Bow":
        if Action == "Basic Bow":
            hit = random.randit(1, 4)
            if hit > 1:
                print ("you plunge your arrow into the", encounter, "dealing 10 damage")
    elif Action == "basic bow":
        if Action == "Basic Bow":
            hit = random.randit(1, 4)
            if hit > 1:
                print ("you plunge your arrow into the", encounter, "dealing 10 damage")
    elif Action == "bow":
        if Action == "Basic Bow":
            hit = random.randit(1, 4)
            if hit > 1:
                print ("you plunge your arrow into the", encounter, "dealing 10 damage")
elif Action == "magic":
    choice = input("Your spells are: fire, and heal")
    if choice == " fire":
        choice = "fire"
    if choice == "fire":
        hit = random.randint(1, 5)
        if hit > 1:
            print ("Forming a ball of fire in your hand you hurl it at the", encounter, "dealing 15 damage")
            enemyhp = enemyhp - 15
        else:
            print ("you miss!")
    if choice == " heal":
        choice == "heal"
    if choice == "heal":
        if hp != maxhp:
            heal = random.randint(5, 15)
        else:
            print ("your health is already full")

0 votes
answered Apr 1 by BhawnaDudeja Dudeja (140 points)
maxhp = int(50)
hp = int(50)
import random
import time
print ("welcome to Ryder's house of monstrositys!!!")
time.sleep (2)
print ("can you escape?")
time.sleep (1)
difficulty = input ('what difficulty would you like, easy normal or hard?')
print ("... preparing dungeon ...")
time.sleep (2)
print (".")
time.sleep (0.5)
print (".")
time.sleep (0.5)
print (".")
time.sleep (0.5)
if difficulty == " easy":
     print ('first encounter: Beatle!')
     encounter = "Beatle"
elif difficulty == "easy":
   print ('first encounter: Beatle!')
   encounter = "Beatle"
elif difficulty == " normal":
    print ("first encounter: Goblin!")
    encounter = "Goblin"
elif difficulty == "normal":
    print ("first encounter: Goblin!")
    encounter = "Goblin"
elif difficulty == " hard":
    print ("first encounter: Kobold ")
    encounter = "Kobold"
elif difficulty == "hard":
    print ("first encounter: Kobold ")
    encounter = "Kobold"
else:
    print ("ERROR INVALID DIFFICULTY BEEP BOOP BOOM")
    exit ()
if difficulty == "easy":
    enemyhp = int(20)
elif difficulty == ' easy':
    enemyhp = int(20)
elif difficulty == "normal":
    enemyhp = int(50)
elif difficulty == " normal":
    enemyhp = int(50)
elif difficulty == 'hard':
    enemyhp = int(100)
elif difficulty == " hard":
    enemyhp = int(100)
choice = input ("Run or Fight?")
if choice == " run":
    print ("GAME OVER!")
    exit ()
elif choice == "run":
    print ("GAME OVER!")
    exit ()
elif choice == (" fight"):
    print ("Battle Begin!")
elif choice == ("fight"):
  print ("Battle Begin!")
hit = int(7)
inventory = "empty"
print ("Your Turn!")
Action = input("what would you like to do: attack, magic, inventory, or retreat")
if Action == "attack":
    Action = input("here are your weapons: Basic sword, and Basic bow")
    if Action == "basic sword":
        Action = "Basic Sword"
    elif Action == "Basic sword":
        Action = "Basic Sword"
    elif Action == "Sword":
        Action = "Basic Sword"
    elif Action == "Sword":
        Action = "Basic Sword"
    if Action == "Basic Sword":
        hit = random.randint(1, 4)
        if hit > 1:
            print ("you slash through the",encounter, "dealing 10 damage!")
    if Action == "Basic Bow":
        hit = random.randit(1, 4)
        if hit > 1:
            print ("you plunge your arrow into the", encounter, "dealing 10 damage")
elif Action == " attack":
    Action = input("here are your weapons: Basic sword, and Basic bow")
    if Action == "Basic Sword":
        hit = random.randint(1, 4)
        if hit > 1:
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "Sword":
        hit = random.randint(1, 4)
        if hit > 1:
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "basic sword":
        hit = random.randint(1, 4)
        if hit > 1:
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "sword":
        hit = random.randint(1, 4)
        if hit > 1:
            print ("you slash through the",encounter, "dealing 10 damage!")
    elif Action == "Basic Bow":
        hit = random.randit(1, 4)
        if hit > 1:
            print ("you plunge your arrow into the", encounter, "dealing 10 damage")
    elif Action == "Bow":
        if Action == "Basic Bow":
            hit = random.randit(1, 4)
        if hit > 1:
            print ("you plunge your arrow into the", encounter, "dealing 10 damage")
    elif Action == "basic bow":
        if Action == "Basic Bow":
            hit = random.randit(1, 4)
        if hit > 1:
            print ("you plunge your arrow into the", encounter, "dealing 10 damage")
    elif Action == "bow":
        if Action == "Basic Bow":
            hit = random.randit(1, 4)
        if hit > 1:
            print ("you plunge your arrow into the", encounter, "dealing 10 damage")
elif Action == "magic":
    Choice = input("Your spells are: fire, and heal")
    if choice == " fire":
        choice = "fire"
    if Choice == "fire":
        hit = random.randint(1, 5)
        if hit > 1:
            print ("Forming a ball of fire in your hand you hurl it at the", encounter, "dealing 15 damage")
            enemyhp = enemyhp - 15
        else:
            print ("you miss!")
    if choice == " heal":
        choice == "heal"
    if choice == "heal":
        if hp == maxhp:
            heal = random.randint(5, 15)
        else:
            print ("your health is already full")
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...