1

I'm trying to get this block of code to determine planet type based off of distance from the star and the only one it will print out the info for is the terraform planet.

If I have it print planet type before the third if statement it will print all the planet type's it chooses, but the only if statement it seems to follow through on is "if planet_type is "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]

I get the following output:

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Xariec
  • 187
  • 1
  • 1
  • 7

1 Answers1

6

Change all is to ==, e.g.:

if planet_type == "mineral":

While using is might work in some cases, the correct way to compare strings is by using ==.

To give some more background, is compares object identity rather than their contents. Two string literals containing the same text may or may not have the same identity.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Source: http://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs – juan Nov 28 '12 at 18:57