Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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. –  sberry Jun 12 at 5:13

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
 
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. –  Moonraker Jun 13 at 4:02

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.