+ 5
How to correctly execute this code?
def unsorted(): import random ls = [] f = open('Story.txt', 'w') for i in range(0,5): num = random.randrange(10,50) ls.append(num) f.write(str(ls)) print('Data saved in file') f.close() def Sorted(): import ast f = open('Story.txt', 'r+') line = f.readline() data = ast.literal_eval(data) #what to put in place of data data.sort() f.write(str(data)) f.close() unsorted() Sorted()
4 Answers
+ 5
Thanks ~swim~
def unsorted():
import random
ls = ""
f = open('Story.txt', 'w')
for i in range(0,5):
num = random.randint(10,50)
ls += str(num)
f.write(str(ls))
print('Data saved in file')
f.close()
def Sorted():
import ast
f = open('Story.txt', 'r+')
line = f.readline()
data = [ast.literal_eval(i) for i in line]
data.sort()
f.write(str(data))
f.close()
unsorted()
Sorted()
Now its working.