Multiple Statements error?

This is the place for queries that don't fit in any of the other categories.

Multiple Statements error?

Postby Mr.Gee » Sat Mar 09, 2013 3:17 pm

Hello. This is my first Python project and I don't know where I messed up.

This is what I get when I run it:
SyntaxError: multiple statements found while compiling a single statement

Code: Select all
# Hero battle with troll
# First Python Project
# 09/03/13

import random

health = 200
mana = 100
troll = 500
mmana = random.randint(1, 10)
sdamage = random.randint(2, 20)
mdamage = random.randint(7, 25)
ssdamage = random.randint(5, 15)
mmdamage = random.randint(5, 15)

print("Your lone Hero faces the final boss. ")
print("Your hero unsheathes his sword for what could be the last fight of his life.\n ")

attack = input("Type which attack you do, melee or magic?: ")
if attack == "melee":
    print("Your hero dishes out", sdamage "but takes", ssdamage, "in return ")
    health -=ssdamage
   
if attack == "magic":
    print("Your hero shoots a fire ball and dishes out ", mdamage "but takes ", mmdamage "in return ")
    health -=mmdamage
    mana -=10

print("You regenerated", mmana ")
print("Your health is now,", health ")
print("Your mana is now,", mana ")
     
input("\nType which attack you do, melee or magic?: ")


Any help would be appreciated.
Mr.Gee
 
Posts: 4
Joined: Sat Mar 09, 2013 3:11 pm

Re: Multiple Statements error?

Postby ichabod801 » Sat Mar 09, 2013 6:02 pm

Really? I get tons of syntax errors. You're missing several commas in your print calls in the conditionals, and you have extra double-quotes in the print calls after the conditionals. Once I cleaned those up it worked okay, but your next step needs to be learning about loops.

Oh, and next time please give the full text of the error message (it's called the traceback).
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Multiple Statements error?

Postby Mr.Gee » Sat Mar 09, 2013 6:45 pm

Thanks I managed to fix the error.
I have improved it but the only problem I have is creating a loop. After the first attack, it should prompt me again and again until the Trolls health is 0.
Here is the code.

Code: Select all
# Hero battle with Troll
# First Python Project
# 09/03/13
# Made by Mr.Gee

import random

#Variables
health = 200
mana = 100
troll = 500
mmana = random.randint(1, 10) # Regeneration mana
sdamage = random.randint(2, 20) #Melee damage
mdamage = random.randint(7, 25) #Magic damage
ssdamage = random.randint(5, 15) #Damage taken by using melee
mmdamage = random.randint(5, 15) #Damage taken by using magic

#Beginning Text
print("Your lone Hero faces the final boss. ")
print("Your hero unsheathes his sword for what could be the last fight of his life.\n ")


#Attack
attack = input("Type which attack you do, melee or magic?: ")
if attack == "melee":
    print("\nYour hero dishes out", sdamage,"damage but takes", ssdamage,"damage in return. ")
    health -=ssdamage
    troll -=sdamage
   
if attack == "magic":
    print("\nYour hero shoots a fire ball and does", mdamage,"damage but takes", mmdamage,"damage in return. ")
    health -=mmdamage
    mana -=10
    troll -=mdamage
    mana += mmana

#Stats after battle
print("Your health is now", health)
if mana <= 99:
    print("You regenerated", mmana, "mana")
print("Your mana is now", mana)
print("The Trolls health is now", troll)


#Ending
if troll <= 0:
    print("Congratulations! You have defeated the Troll!")
Mr.Gee
 
Posts: 4
Joined: Sat Mar 09, 2013 3:11 pm

Re: Multiple Statements error?

Postby Yoriz » Sat Mar 09, 2013 6:58 pm

New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 721
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Multiple Statements error?

Postby ichabod801 » Sat Mar 09, 2013 8:23 pm

Here's a simple program:

Code: Select all
troll_hits = 5
while troll_hits:
    print('The troll has {} hit points left.'.format(troll_hits))
    attack = input('Press return to hit the troll: ')
    troll_hits -= 1
print('You win!')


Read what Yoriz linked to, use it to understand the simple program, then work your more complicated program into the simple program.

Babing bada.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Multiple Statements error?

Postby Mr.Gee » Sat Mar 09, 2013 8:53 pm

Thanks for the replies but I am still confused.
The while loop needs to repeat this section of the code.
Code: Select all
#Attack
attack = input("Type which attack you do, melee or magic?: ")
if attack == "melee":
    print("\nYour hero dishes out", sdamage,"damage but takes", ssdamage,"damage in return. ")
    health -=ssdamage
    troll -=sdamage
   
if attack == "magic":
    print("\nYour hero shoots a fire ball and does", mdamage,"damage but takes", mmdamage,"damage in return. ")
    health -=mmdamage
    mana -=10
    troll -=mdamage
    mana += mmana

#Stats after battle
print("Your health is now", health)
if mana <= 99:
    print("You regenerated", mmana, "mana")
print("Your mana is now", mana)
print("The Trolls health is now", troll)


But when I type this code, the attacking damage and receiving damage stays the same every time.
Code: Select all
while troll:
    attack = input("\nType which attack you do, melee or magic?: ")
    attack == "melee"
    print("\nYour hero dishes out", sdamage,"damage but takes", ssdamage,"damage in return. ")
    health -=ssdamage
    troll -=sdamage
    print("\nYour health is now", health)
    print("Your mana is now", mana)
    print("The Trolls health is now", troll)


It needs to come up with this prompt every round and continue until the trolls health is 0 or less.
Code: Select all
attack = input("Type which attack you do, melee or magic?: ")


I'm very sorry for bothering you as I just started Python today.
Mr.Gee
 
Posts: 4
Joined: Sat Mar 09, 2013 3:11 pm

Re: Multiple Statements error?

Postby ichabod801 » Sat Mar 09, 2013 9:46 pm

If you want those values to change every time the loop iterates (goes through one cycle), then you need to put the statements that assign random numbers to them inside the loop. This:

Code: Select all
import random
x = random.randrange(5)
for i in range(5):
    print(x)


vs. this:

Code: Select all
import random
for i in range(5):
    x = random.randrange(5)
    print(x)
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Multiple Statements error?

Postby Mr.Gee » Mon Mar 11, 2013 6:27 pm

Updated my code into the while loop.
Code: Select all
# Hero battle with Troll
# First Python Project
# Made by Mr.Gee

#Variables
import random   
running = True
health = 200    #Health of hero
mana = 100      #Mana of hero
troll = 200     #Trolls health
mmana = random.randrange(1, 10)    #Regeneration mana
sdamage = random.randrange(5, 20)  #Melee damage
mdamage = random.randrange(7, 25)  #Magic damage
ssdamage = random.randrange(5, 15) #Damage taken by using melee
mmdamage = random.randrange(5, 10) #Damage taken by using magic

#Beginning Text
print("Your lone Hero faces the final boss. ")
print("Your hero unsheathes his sword for what could be the last fight of his life.\n ")
print("Instructions")
print("Type melee to do a sword attack or type magic to shoot a fire ball.")
print("Magic does more damage than melee but uses up mana.")
print("Mana is regenerated every round.")

#Main body of the text - Repeated loop
while running:
   attack = input('\nType which attack you do, melee or magic?: ')
   if attack == 'magic':
            print('\nYour hero shoots a fire ball and does', mdamage,'damage but takes', mmdamage,'damage in return. ')
            health -=mmdamage
            mana -=10
            troll -=mdamage
            mana += mmana
            print('Your health is now', health)
            print('You regenerated', mmana,'mana')
            print('Your mana is now', mana+mmana)
            print('The Trolls health is now', troll)
   elif attack == 'melee':
            print('\nYour hero dishes out', sdamage,'damage but takes', ssdamage,'damage in return. ')
            health -=ssdamage
            troll -=sdamage
            print('Your health is now', health)
            print('Your mana is now', mana)
            print('The Trolls health is now', troll)


Things left to change
1) I still don't understand how to make the values change every time the loop cycles, so the damage changes every time.
2) If the player has less than 10 mana, you cannot do the magic attack. It will display a message to tell you.
3) When the Troll has less than or equal to 0 health, the loop breaks and prints a statement. E.g. ('Congrats you won the game'). And vice versa with the health. E.g. ('Your player collapses in a pool of blood') etc.

If anyone can help me with any of the problems above it will be much appreciated.
Mr.Gee
 
Posts: 4
Joined: Sat Mar 09, 2013 3:11 pm

Re: Multiple Statements error?

Postby ichabod801 » Tue Mar 12, 2013 12:30 am

Mr.Gee wrote:Things left to change
1) I still don't understand how to make the values change every time the loop cycles, so the damage changes every time.


I gave an example in my previous post, what don't you understand about it? In general, if you want something to happen every loop cycle you have to put it into the loop. That is, put the statement into the indented block under you loop statement (in this case, "while running:").

Mt.Gee wrote:2) If the player has less than 10 mana, you cannot do the magic attack. It will display a message to tell you.


For this you need to expand your conditional structure. Currently you have something like:

Code: Select all
if a:
    do x
elif b:
    do y


If you only want to do x when both a and c are true, there's a couple ways you can do this. One way is:

Code: Select all
if a:
    if c:
        do x
    else:
        print("I can't do x")
elif b:
    do y


That's a bit more clear, but this does the same thing with less code:

Code: Select all
if a and c:
    do x
elif a and not c:
    print("I can't do x")
elif b:
    do y


Mr.Gee wrote:3) When the Troll has less than or equal to 0 health, the loop breaks and prints a statement. E.g. ('Congrats you won the game'). And vice versa with the health. E.g. ('Your player collapses in a pool of blood') etc.


Currently your loop runs forever. This is a common error with while loops. You have said "while running", but then you never change the value of running. You have to change the value of running. You just need another pair of conditionals after the ones you've got. The first one checks the troll, prints you've won, and sets running to False. The second checks the player, prints you've lost, and sets running to False.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest