我在玩 Python,正在制作一个小行星模拟器。我有一个部分应该接收一个值并基于该值返回第二个值。我花了几个小时来解决这个问题,但我完全被难住了。我添加了一些打印消息来帮助我跟踪值,它似乎应该工作,但不是。我几乎可以肯定这是显而易见的。我究竟做错了什么?
这是有问题的代码:
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)
作为示例,这是我得到的输出示例:
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
在我看来,pType 4 应该跳过前两个 IF 语句并从第三个语句接收一个值,但看起来它被 1 或 2 捕获了。有什么想法吗?