This is a assignment I have to do for school.

The program is about attack and defend. This is how it suppose to look like when it outputs: http://i43.tinypic.com/bew6q.jpg

Mine is basically done accept the part where it says "Your Health is:" the health is suppose to be "health = 20" when it starts off.. when it attacks the random die will roll and the health will "Health = health - x" where x = die roll (random number) but it's not showing or working....

This is mine:

health = 20

import random

def Dieroll():
 randomnumber = random.randrange(10)+1
 return randomnumber

def Healthmod():
 x = Dieroll()
 global health
 health = health - x

#main


while True:
 action = raw_input("Do you want to Attack or Defend? ")
 if action == "attack":
   print "You are attacking!"
   print "Your health is ", health
 elif action == "defend":
   print "You are defending!"
 else:
   print "Type 'attack' or 'defend'!"

print "Your health is ", health

if health < 0:
    
 print "You're dead."

raw_input ("Press a key to exit.")

Recommended Answers

All 6 Replies

where do you call your functions?

Yeah.. all you're doing is printing a bunch of things. You fail to call any health-affecting functions

where do you call your functions?

This is the function unit... so i call this functions.

Yeah.. all you're doing is printing a bunch of things. You fail to call any health-affecting functions

What do you mean?
I stated the health = 20 the effects that will happen. Like health = health - x , when blah blah. I don't understand what's wrong.

your code execution starts from while True: (and since you had #main, it seems you understand that). read your code from that point onwards, where ever would health change?

Let me re-write your program to show you what you're actually doing. Maybe you'll understand the execution of Python code a little better in this form:

import random

def Dieroll():
 randomnumber = random.randrange(10)+1
 return randomnumber

def Healthmod():
 x = Dieroll()
 global health
 health = health - x

def main():
    # Here comes an infinite loop with no breaks
    while True:
        action = raw_input("Do you want to Attack or Defend? ")
        if action == "attack":
            # Two print statements
            print "You are attacking!"
            print "Your health is ", health
        elif action == "defend":
            # One print statement
            print "You are defending!"
        else:
            # One print statement
            print "Type 'attack' or 'defend'!"
        # End of loop... return to beginning...

    # Still only printing
    print "Your health is ", health

    # Checking health, but still not calling functions
    if health < 0:
        print "You're dead."

    raw_input ("Press a key to exit.")

# The above function declarations are simply declarations
# Up until this point, no code has been executed
if __name__ == '__main__':
    # The above makes sure that this script isn't simply
    #   being called by another script..
    # Now we'll start our main execution by calling the
    #   main() function:
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.