Chris Carter
Chris Carter

Reputation: 21

Need help figuring out while my function isn't working (PYTHON)

I am new to Python coding and I'm trying to write a short text-based adventure game. I'm trying to write a function that takes the current health and subtracts the monsters value given by a random call. Here's what I have.

import random

monster = 0
health = 100

def monEnc(monster,health):
    monster = random.randint(0,21)
    health = health - monster
    print(health)
    return

monEnc()

When I try and run the code, monEnc() does nothing and I'm kind of lost.

Upvotes: 0

Views: 68

Answers (2)

Justin
Justin

Reputation: 902

This should work, you were not passing the monster and health parameters to your function which caused the error. The monster variable is created inside your function so does not need to be passed as a parameter.

import random

health = 100

def monenc(health_points):
    monster = random.randint(0,21)
    health = health_points - monster

print(monenc(health))
print(monenc(health))

Upvotes: 0

Heretohelp
Heretohelp

Reputation: 51

Your function "monEnc" takes 2 parameters, while you pass it none. If you don't want it to return anything, don't include return at all. You probably want something like:

import random

def monEnc(monster,health):
    health = health - monster
    print(health)
    return health

monEnc(0, 100)

or

import random

def monEnc(health):
    monster = random.randint(0,21)
    health = health - monster
    print(health)
    return health

monEnc(100)

Upvotes: 5

Related Questions