Your example is really not stupid! Remember, even the best of us were stupid about Python at one time ...
a = 0
while a < 10:
a = a + 1
if a == 4 or a == 6:
# create file for instance 'output4.dat'
# only strings concatinate
output = open('output'+str(a)+'.dat', 'w')
# write the value to file (needs to be a string)
output.write('%s' % (a))
output.close()
The best way to learn is to study other folks code and experiment with it.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
hello, how would i change the code here, to have python create variables that i can call later, instead of files?
The best way is to create a dictionary instead of variables
from pprint import pprint
dic = dict()
for a in range(50):
if a % 10 in (4, 6):
dic["output%d" % a] = a
pprint(dic)
""" my output -->
{'output14': 14,
'output16': 16,
'output24': 24,
'output26': 26,
'output34': 34,
'output36': 36,
'output4': 4,
'output44': 44,
'output46': 46,
'output6': 6}
"""
Edit: don't revive old threads, start a new one instead.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691