我正在尝试使用这段代码来确定基于距离恒星距离的行星类型,并且唯一一个它将打印出来的信息是地球行星。
如果我在第三个if语句之前打印行星类型它将打印它选择的所有行星类型,但它似乎贯穿的唯一if语句是“如果planet_type是”terraform“:
for distance in (a):
while True:
random_x = random.randint(-distance,distance)
random_y = random.randint(-distance,distance)
if(random_x <distance and random_x >-distance and random_y <distance and random_y >-distance):
continue
else:
print "Creating planets"
time.sleep(.5)
print distance
if star_size*100 >= distance:
possible_planet_type = ('mineral' , 'gas', 'organic', 'terraform', 'ice')
planet_type = random.choice(possible_planet_type)
if planet_type is "mineral":
planet_diameter = random.randint(3000,8000)
iron = planet_diameter*random.randint(10,100)
carbon = planet_diameter*random.randint(5,50)
oxygen = planet_diameter*random.randint(0,0)
h2o = planet_diameter*random.randint(0,1)
deuterium = planet_diameter*random.randint(0,5)
helium_3 = planet_diameter*random.randint(0,2)
print planet_type, planet_diameter, iron, carbon, oxygen, h2o, deuterium, helium_3
# name = a[distance]
elif planet_type is "organic":
planet_diameter = random.randint(8000,10000)
iron = planet_diameter*random.randint(10,20)
carbon = planet_diameter*random.randint(5,10)
oxygen = planet_diameter*random.randint(40,100)
h2o = planet_diameter*random.randint(30,100)
deuterium = planet_diameter*random.randint(0,3)
helium_3 = planet_diameter*random.randint(0,2)
print planet_type, planet_diameter, iron, carbon, oxygen, h2o, deuterium, helium_3
# name = a[distance]
elif planet_type is "terraform":
planet_diameter = random.randint(8000,10000)
iron = planet_diameter*random.randint(10,20)
carbon = planet_diameter*random.randint(5,10)
oxygen = planet_diameter*random.randint(40,100)
h2o = planet_diameter*random.randint(30,100)
deuterium = planet_diameter*random.randint(0,3)
helium_3 = planet_diameter*random.randint(0,2)
print planet_type, planet_diameter, iron, carbon, oxygen, h2o, deuterium, helium_3
# name = a[distance]
elif planet_type is "gas":
planet_diameter = random.randint(20000,90000)
iron = planet_diameter*random.randint(10,20)
carbon = planet_diameter*random.randint(5,10)
oxygen = planet_diameter*random.randint(40,100)
h2o = planet_diameter*random.randint(30,100)
deuterium = planet_diameter*random.randint(0,0)
helium_3 = planet_diameter*random.randint(0,2)
print planet_type, planet_diameter, iron, carbon, oxygen, h2o, deuterium, helium_3
# name = a[distance]
elif planet_type is "ice":
planet_diameter = random.randint(2000,6000)
iron = planet_diameter*random.randint(10,20)
carbon = planet_diameter*random.randint(5,10)
oxygen = planet_diameter*random.randint(40,100)
h2o = planet_diameter*random.randint(30,100)
deuterium = planet_diameter*random.randint(0,1)
helium_3 = planet_diameter*random.randint(0,1)
print planet_type, planet_diameter, iron, carbon, oxygen, h2o, deuterium, helium_3
# name = a[distance]
我得到以下输出:
Creating planets
6199
terraform 8578 162982 77202 737708 660506 17156 17156
Creating planets
4664
Creating planets
9844
Creating planets
4410
terraform 8280 99360 41400 819720 571320 0 0
Creating planets
7483
Creating planets
7882
Creating planets
2111
Creating planets
978
Creating planets
1857
terraform 9446 122798 94460 765126 425070 9446 9446
Creating planets
9155
Creating planets
1093
Creating planets
8646
Creating planets
5313
答案 0 :(得分:6)
将所有is
更改为==
,例如:
if planet_type == "mineral":
虽然在某些情况下使用is
可能,但使用==
比较字符串的正确方法。
为了提供更多背景信息,is
会比较对象身份而不是其内容。包含相同文本的两个字符串文字可能具有相同的标识,也可能不具有相同的标识。