0

What's wrong with this code?

a="ass1.txt" with open(a,"w")as f: nn=int(input("enter no of elements :")) for i in range(0,nn): vv=input("enter element :") if (i<nn-1): f.write(vv) f.write("\n") else: f.write(vv) with open (a,"r")as f: x=f.readlines() x.sort() print(x) n=len(x) print("Largest element is:",x[n-1])

27th Jan 2019, 12:59 PM
Mohd Adnan
Mohd Adnan - avatar
6 Respostas
+ 2
You don't really need to distinguish between the last element and all other elements. You can just go for i in range(0,nn): vv=input("enter element :") f.write(vv + '\n') It doesn't make any difference that a line break will be appended to the last element. I think your problem is that '\n' is part of each element and gets printed when you print the sorted items? You can get rid of those by adding something like x = [e.strip() for e in x] after the readlines method. Or use this instead: with open (a,"r")as f: x = [line.strip() for line in f.readlines()]
27th Jan 2019, 1:11 PM
Anna
Anna - avatar
+ 2
I see. That's because the numbers you enter are treated as strings and sorted lexicographically which means that they are sorted like words in a dictionary: 1 100 12 2 234 29 Here, the largest number would be 29 (which is obviously wrong). To change that, convert the numbers to integers: x = [int(e.strip()) for e in x] Note that this will raise an exception if you enter strings that cannot be converted to integers.
27th Jan 2019, 1:54 PM
Anna
Anna - avatar
+ 1
Thank you so much mam for helping me, now the code working fine New code : a="ass1.txt" with open(a,"w")as f: nn=int(input("enter no of elements :")) for i in range(0,nn): vv=input("enter element :") if (i<nn-1): f.write(vv) f.write("\n") else: f.write(vv) with open (a,"r")as f: v=f.readlines() list1=[] for i in v: o=int(i) list1.append(o) list1.sort() print(list1) n=len(list1) print("Largest element is:",list1[n-1]) Mam can you please give me some tips about how to write a good program with a proper structure.. And can u please give some idea about how can i reduce size of above code Thank you!
27th Jan 2019, 2:13 PM
Mohd Adnan
Mohd Adnan - avatar
+ 1
The structure looks fine. You can make the code a little more compact like this: a="ass1.txt" with open(a, "w") as f: nn = int(input("enter no of elements: ")) for i in range(nn): vv = input("enter element: ") f.write(vv + '\n') with open (a, "r") as f: x = sorted([int(line.strip()) for line in f]) print("Largest element is: ", x[-1])
27th Jan 2019, 2:19 PM
Anna
Anna - avatar
+ 1
By the way, you can open the file in w+ mode so you can both write to and read from the file without having to close it: a="ass1.txt" with open(a, "w+") as f: nn=int(input("enter no of elements: ")) for i in range(nn): vv=input("enter element: ") f.write(vv + '\n') f.seek(0) # jump back to the beginning of the file x = sorted([int(line.strip()) for line in f]) print("Largest element is:",x[-1])
27th Jan 2019, 2:24 PM
Anna
Anna - avatar
0
Thanks for your concern But actually my problem is improper output Can u please try this code once Add some no's to list And check if the output is the Greatest no from the list When i added elements as follows: 2345 123 556 234 564 It gave me output as 564 as the greatest no in list ☹️
27th Jan 2019, 1:35 PM
Mohd Adnan
Mohd Adnan - avatar