'Python script unable to run from console but is running fine from Idle
After F5 in Idle the input prompt appears, I enter a number the file is generated - everything works.
If I try to execute the script directly from the file a console is opened and closed with no result.
If I move the f = open("galaxies.txt","w+") before the outer most FOR loop the script doesn't work at all.
Any suggestions for a fix and reasons why it behaves like this?
import string, random
f = open("galaxies.txt","w+")
def inputNumber(message):
while True:
try:
howmany = int(input(message))
except ValueError:
print("Not an integer! Try again.")
continue
else:
return howmany
break
string.ascii_letters
type_galaxy = ['elliptical','spiral','irregular']
howmany = inputNumber('How many galaxies to generate? ')
for i in range(howmany):
planet_names = []
pos_0 = 'g' + random.choice(string.ascii_letters).upper() + random.choice(string.ascii_letters).upper() + random.choice(string.ascii_letters).upper() + '-' + random.choice(string.ascii_letters).upper() + str(random.randint(1, 9999))
gal_type = type_galaxy[random.randint(0,2)]
num_of_planets = random.randint(1,43)
for k in range(num_of_planets):
pos_1 = 'p' + random.choice(string.ascii_letters).upper() + random.choice(string.ascii_letters).upper() + '-' + random.choice(string.ascii_letters).upper() + str(random.randint(1, 99))
planet_names.append(pos_1)
if i < 1:
f.write('-----Galaxy '+pos_0+'----- \n')
else:
f.write('\n-----Galaxy '+pos_0+'----- \n')
f.write('Type: '+gal_type + ' \n')
f.write('There are '+str(num_of_planets)+' planets in the galaxy. Their names are: \n')
for x in planet_names:
f.write (x + ' and it has ' + str(random.randint(0,56)) + 'moons \n')
f.write('-----Thats all from Galaxy '+pos_0+'----- \n')
f.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
