mercredi 30 août 2017

Unable to counter a loop via enumerate() in Python 3 due to 'TypeError: 'int' object is not iterable'

Note: this is my first program i've ever written and first time posting to SO, so if I've committed some grave sin please let me know.

I'm attempting to write a program that will generate a random number (planet_seed), generate a random number that many times, and then increment a counter once for that.

For example, if planet_seed was 2, I would need to generate one random number (planet_type), increment a counter, and then add that counter to the second random number; so if on the first loop, planet_type would be + 0, then + 1 on the next, and + 2 on the next, and so forth.

What I have now is as such. I've included the whole code since I frankly have no clue what's going on.

import random

# determine single, bi-, tri- system

system_type = random.randint(1, 6)
system_struct = ['null', 'single', 'single', 'single', 'binary', 'binary', 
'trinary']
print('This is a ' + (system_struct[system_type]) + ' star system.')
if system_struct[system_type] == 'single':
    star_num = 1
if system_struct[system_type] == 'binary':
    star_num = 2
if system_struct[system_type] == 'trinary':
    star_num = 3

# determine star types

star_list = ['null','white main sequence', 'white dwarf', 'yellow Main 
Sequence', 'red Carbon Star', 'red giant', 'orange main sequence', 'red 
dwarf', 'red supergiant', 'brown dwarf', 'methane dwarf', 'ultraviolet 
luminous', 'ultraviolet supergiant', 'blue luminous', 'dying supergiant',
'neutron star', 'quark star', 'black hole', 'dark star', 'boson star', 'ice 
star']

for a in range(star_num):
    star_type = random.randint(1,21)
    print('The star is a ' + (star_list[star_type]) +'.')

# determine number of planets
planet_seed = random.randint(2,12)

for counter,b in enumerate(planet_seed):
   planet_type = random.randint(1,6)

When I run this, I get

  File "C:/Users/Cinaed/PycharmProjects/NEW System Generator/Star System 
Type.py", line 28, in <module>
    for counter,b in enumerate(planet_seed):
TypeError: 'int' object is not iterable

spat back at me. Any insights?




Aucun commentaire:

Enregistrer un commentaire