user10335603
user10335603

Reputation:

Difficulty with writing short RPG Script

Write a function named "compute_xp" that takes two parameters which are an integer and boolean in that order. The function will return the 44993 if the boolean is false (lost the battle) or 44993 plus the integer parameter if the boolean is true (won the battle)

def compute_xp(i, b):
    if b == 44993:
        return 44993 
    else:
        return 44993 + i

This is my code, but I keep getting an incorrect answer when I input a value to make the statement true.

Upvotes: 1

Views: 166

Answers (4)

Danny
Danny

Reputation: 19

try this. Remember a boolean can only have one of two values true or false so it can never be set equal to a number.

def compute_xp(x,y):
    if y == True:
        return (x + 44993)
    else:
        return 44993

Upvotes: 0

Swift
Swift

Reputation: 1711

I think you should re-think this.

Here is what I would do:

def compute_xp(int_value, bool_value):
    if b == True:
        #If the battle is won run this code block
        #return 44993, int_value (this would return two seperate things," (44993, int_value) ")
        return 44993 + i
    else:
        #If the battle is lost, run this code
        return 44993

The reason you code does not work, is because you are trying to interpret a Boolean value as an integer, if b == 44993 which of course, will always be false, because neither True nor False are == 44993.

So what my code does, is check the value of the boolean explicitly, so only in the event that b == True will it return 44993 + i

I added an extra line to the code in the form of a comment. return 44993 + i doesn't seem like a useful thing to return. Returning 44993, i would return a tuple, index 0 would be an integer and index 1 would be your int_value.

Upvotes: 0

Michael Hurley
Michael Hurley

Reputation: 369

In the original code example, if b == 44993: compares the parameter b to 44993.
Since b is expected to be a boolean, this comparison will always return false.

Here is my solution:

def compute_xp(i, b):
  if b:
      return 44993 + i
  else:
      return 44993  

if b: checks the boolean value of b, so if b is true, then return 44993 + i will be executed. If b is false, then return 44993 will be executed.

Upvotes: 2

pakobill
pakobill

Reputation: 470

Why are you comparing the boolean var with the number? Try:

def compute_xp(i, b):
    if b:
        return 44993 + i
    else:
        return 44993 

or more shortly:

def compute_xp(i, b):
    return 44993 + i if b else 44993

Upvotes: 1

Related Questions