Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
syntax help
#1
I've only just started to learn python, 2 days in.
After going through a few exercises i thought i could rewrite one of them into my own "game"
But I'm getting a syntax error and i don't know enough to know why it's a syntax error.

here is the code to look at https://pastebin.com/hNiuRNn8

thanks in advance
Reply
#2
Just to keep the code in the thread
### shitty battle simulator ###
import random
import time

playerHealth = 50
compHealth = 50

def battleIntro():
    print ("You enter the arena to the cheers of the crowds. ")
    print ("Across fomr you stands your opponent. ")
    print ("Only one you will leave this place alive. ")
    print ("Prepare to fight!" )

def battlePlayer():
    missChance = 2
    while playerHealth >  0 and compHealth > 0:
        print ("Do you wish to attack")
        time.sleep(input())

        miss = random.randint (1, 4)
        
        if (missChance) == (miss):
            print ("You swing wildly and miss your opponent. ")
        else:
            print ("You strike your opponent doing damage. ")
            global compHealth = global compHealth - 5
            ###syntax error on the = ###
            
def battleComp():
     missChance = 2
    while playerHealth >  0 and compHealth > 0:
        print ("Your opponent charges at you. ")
        time.sleep(2)

        miss = random.randint (1, 4)
        
        if (missChance) == (miss)
            print ("You dodge his attack. ")
        else:
            print ("He manages to strike you doing damage. ")
            global playerHealth = global playerHealth - 5

def battleResult():
    if global comphealth == 0:
        print ("Your hands are slick with his blood. ")
        print ("You have slayen your opponent." )

    or if global playerHealth == 0:
        print ("Your opponent stands over you and drives his sword into your chest. ")
        print ("You have died! ")

playAgain = "yes"
while playAgain == "yes" or playAgain == "y":

    battleIntro()
    ### unsure of what needs to go here ###
    
    print ("Do you want to play again?")
    playAgain = input()
please, always post the full traceback you get, in error tags
Always use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The example code that @buran posted from the URL you provided has several syntax errors and several logic errors. Your first job is to get the code to compile without any errors or warnings with as few changes as possible. The following list should help you get started:
a. The use of 'global' is discouraged.
b. Variable names should be lower case (e.g. comp_health instead of compHealth. NOTE: CASE MATTERS.
c. Indentation matters
d. Colons at the end of if .. elif .. else statements
e. Use of unneeded parentheses is discouraged
f. 'or if' should be 'elif'
g. print ("Do you wish to attack") should probably by choice = input("Do you wish to attack?")
h. Put lots of print statements like print("battleComp(): playerHealth = {} compHealth = {}.".format(playerHealth, compHealth)) so you know what is going on during testing.

The following code snippets should help you with the notes above.

Use of global (not recommended - see alternative example below):
def battleResultExampleUsingGlobalWhichShouldBeDiscouraged():
    global compHealth
    global playerHealth
    if compHealth == 0:
        print ("Your hands are slick with his blood. ")
        print ("You have slayen your opponent." )
 
    elif playerHealth == 0:
        print ("Your opponent stands over you and drives his sword into your chest. ")
        print ("You have died! ")
Alternative to use of global:
def example(player_health, opponent_health):
    player_health = 33    
    opponent_health = 66
    return player_health, opponent_health

player_health = 99
opponent_health = 98
print("before: player_health = {}   opponent_health = {}.".format(player_health, opponent_health))
player_health, opponent_health = example(player_health, opponent_health)
print("after:  player_health = {}   opponent_health = {}.".format(player_health, opponent_health))
Main body of program example. There are better ways to do the loop, but this should help you get started.
playAgain = "yes"
while playAgain == "yes" or playAgain == "y":
 
    battleIntro()
    ### unsure of what needs to go here ###
    battlePlayer()
    battleComp()
    battleResult()
     
    print ("Do you want to play again?")
    playAgain = input()
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
This is how i got it to work.
If anyone has a better way to achieve the same results please let me know.
thanks to @ljmetzger for pointing me in the right direction

### shitty battle simulator ###
import random
import time

playerHealth = 50
compHealth = 50

def battleIntro():
    print ("You enter the arena to the cheers of the crowds. ")
    print ("Across fomr you stands your opponent. ")
    print ("Only one you will leave this place alive. ")
    print ("Prepare to fight!" )
    print()
    
def battlePlayer():
    global compHealth
    global playerHealth
    missChance = 2
    while playerHealth >  0 and compHealth > 0:
        print ("Do you wish to attack")
        #input ("enter")

        miss = random.randint (1, 4)
        
        if (missChance) == (miss):
            print ("You swing wildly and miss your opponent. ")
            print()
        else:
            print ("You strike your opponent doing damage. ")
            print()
            compHealth = compHealth - 5
        break
            
def battleComp():
    global playerHealth
    global compHealth
    missChance = 2
    while playerHealth >  0 and compHealth > 0:
        print ("Your opponent charges at you. ")
        #time.sleep(2)

        miss = random.randint (1, 4)
        
        if (missChance) == (miss):
            print ("You dodge his attack. ")
            print()
        else:
            print ("He manages to strike you doing damage. ")
            print()
            playerHealth = playerHealth - 5
        break
        
def battleResult():
    global comphealth
    global playerHealth
    if compHealth == 0:
        print ("Your hands are slick with his blood. ")
        print ("You have slayen your opponent." )
        print()
        
    elif playerHealth == 0:
        print ("Your opponent stands over you and drives his sword into your chest. ")
        print ("You have died! ")
        print()
        
def battleLoop():
    while playerHealth >  0 and compHealth > 0:
        battlePlayer()
        battleComp()
    #if playerHealth < 0 or compHealth < 0:
        #battleResult()
    
playAgain = "yes"  
while playAgain == "yes" or playAgain == "y":
    playerHealth = playerHealth + 50
    compHealth = compHealth + 50
    battleIntro()
    #battlePlayer()
    #battleComp()
    battleLoop()
    battleResult()

    print ("Do you want to play again?")
    playAgain = input()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020