0

I was messing around with Python and was making a little planet simulator. I have one section that is supposed to receive a value and based on that value return a second value. I spent a few hours troubleshooting this, but I'm absolutely stumped. I added some print messages to help me track the values and it seems like it should be working, but isn't. I'm almost sure it is something obvious. What am I doing wrong?

Here is the code in question:

def assignTerrain(pType):

print("Assigning terrain values to planet type: ", planetType[pType], pType)

if pType == 0:
    print("Value 0 assigned", pType)
    return 0

if pType == 1 or 2:
    temp = random.randint(1, 11)
    print("Value %d assigned" % temp, pType)
    return temp

if pType == 3 or 4:
    print("Value 12 assigned", pType)
    return 12

print("There was an error with pType: ", pType)

As a sample, here is an example of the output I'm getting:

Assigning terrain values to planet type:  Asteroid Belt 4
Value 3 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 5 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 4 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 7 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 8 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 2 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 4 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 1 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 9 assigned 4
Assigning terrain values to planet type:  Asteroid Belt 4
Value 8 assigned 4

It seems to me that pType 4 should be skipping the first two IF statements and receive a value from the third, but it looks like it is getting caught by the 1 or 2. Any thoughts?

1
  • 2
    Instead of if pType == 1 or 2 try if pType in (1, 2) or if pType == 1 or pType == 2. Comparators in Python don't work the way you are thinking they do. Commented Jun 12, 2013 at 5:13

2 Answers 2

3

Instead of if

pType == 1 or 2 

try

if pType in (1, 2) 

or

if pType == 1 or pType == 2. 

Comparators in Python don't work the way you are thinking they do. What you are doing actually evaluates to

if ((pType == 1) or (4))

Which, since 4 is truthy, is always True.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I feel so sheepish! I remember thinking that isn't how I'm used to IF working and thought I'd try it new, but somehow never thought to use a more traditional approach. I appreciate your concise and helpful answer.
2

The expression pType == 1 or 2 is the logical union of the two expressions pType == 1, which could be True or False, and 2, which is always True to Python. Thus, the expression pType == 1 or 2 is always True regardless of the value of pType.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.