1

我试图让这个代码块根据与恒星的距离来确定行星类型,唯一会打印出信息的是terraform行星。

如果我让它在第三个 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
4

1 回答 1

6

全部更改is==,例如:

if planet_type == "mineral":

虽然 using在某些情况下is 可能有效,但比较字符串的正确方法是使用==.

为了提供更多背景信息,is比较对象身份而不是它们的内容。包含相同文本的两个字符串文字可能具有也可能不具有相同的标识。

于 2012-11-28T18:42:25.003 回答