How to correctly execute this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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()

1st Sep 2019, 4:17 PM
Pradip Thoyal
Pradip Thoyal - avatar
1 Answer
+ 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.
2nd Sep 2019, 4:26 AM
Pradip Thoyal
Pradip Thoyal - avatar