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?
if pType == 1 or 2
tryif pType in (1, 2)
orif pType == 1 or pType == 2
. Comparators in Python don't work the way you are thinking they do. – sberry Jun 12 at 5:13