-1

I want to get the scores which the user has to input then order the scores in ascending order however I am struggling to do so. Below is my code so far and it doesn't display the scores in order.Thanks in advance and your help is very much appreciated.

classa = input("what class are you in?")

if classa == "1":

    file=open("class 1.csv", "a+")

if classa == "2":

    file=open("room2.csv", "a+")

if classa == "3":

    file=open("class3.csv", "a+")

score1= int(input("Enter the score 1 results: "))

score2= int(input("Enter the score 2 results: "))

score3= int(input("Enter the score 3 results: "))

newrecord =score1,",",score2,",",score3

file.write(newrecord)

file.write("\n")

file.close()


import csv

import operator

with open('room2.csv','r') as csvfile:

    readCSV = csv.reader(csvfile, delimiter=',')

    for row in readCSV:

        print (row)
Louis
  • 146,715
  • 28
  • 274
  • 320
Bob Stanley
  • 49
  • 3
  • 11

2 Answers2

1

You also cannot write a tuple to the file which is what newrecord = score1,",",score2,",",score is creating. You need to make a string from the scores and write that:

newrecord = "{},{},{}\n".formmat(score1,score2,score3)

file.write(newrecord)

To sort existing scores you just need to call sorted on each row, using int as the key to sorted so you values are compared as integers not strings, set reverse=True to order from highest to lowest.

with open('room2.csv','r') as csvfile:  
    readCSV = csv.reader(csvfile)
    for row in readCSV:
        srt_row = sorted(row,key=int,reverse=True)

To sum each row and then sort, sum the values after mapping to ints again setting reverse=True to sort from high to low:

with open('room2.csv') as csvfile:
    readCSV = csv.reader(csvfile)
    srt = sorted((sum(map(int,row)) for row in readCSV), reverse=True)

If you want to write to a file:

with open('room2.csv','r') as csvfile, open("out.csv","w") as out:
    readCSV = csv.reader(csvfile)
    for scre in sorted((sum(map(int, row)) for row in readCSV), reverse=True):
        out.write(str(scre))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
-1

You could use sort() to sort a list.
For example:

# List 
listOne = ['5', '7', '1']
# Sort the list 
listOne.sort()
# Display the results 
print('List', listOne)

You can eather sort the list before writing the record to a csv file, or when reading the csv file add the values to a list and sort those.

Example 2:
Sort before adding the values to the csv file.

# Declare an empty list 
newrecord = [] 

score1= int(input("Enter the score 1 results: "))
score2= int(input("Enter the score 2 results: "))
score3= int(input("Enter the score 3 results: "))

# Add scores 
newrecord.append(score1)
newrecord.append(score2)
newrecord.append(score3)
# Sort list 
newrecord.sort()
# Print result 
print(newrecord)

Example 3:
Sort the values after taking the values from the csv file.

# Declare an empty list. 
csvList = [] 

with open('room2.csv','r') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        # Add value to a list. 
        csvList.append(row)

# Sort the values from the CSV file. 
csvList.sort()

# Print result. 
print(csvList) 
Tenzin
  • 2,415
  • 2
  • 23
  • 36
  • I want the numbers to saved in a csv file then I read the csv file and sort it in ascending order. – Bob Stanley Dec 20 '15 at 19:50
  • You could do that as well. Read the CVS file number, store those in a list and then sort that list. – Tenzin Dec 20 '15 at 19:52
  • Why is my post -1? Topic Starter aks how to sort items, I give a few suggestion and people give me negative feedback and don't tell me why? – Tenzin Dec 20 '15 at 19:58
  • 1
    That wasn't me, I can't upvote nor down vote an answer as I haven't reached 15 reputation – Bob Stanley Dec 20 '15 at 20:01
  • I know. I hope I gave you some clues though. :-)) – Tenzin Dec 20 '15 at 20:02
  • when I tried to run example 3 the output was just [] – Bob Stanley Dec 20 '15 at 20:05
  • Not mine but this would error in python3 and fail silently in python3, you are mixing types – Padraic Cunningham Dec 20 '15 at 20:06
  • I am have tested this code in my python, and it gives no errors. But I did not test the file section. Maybe the file is not being read correctly? – Tenzin Dec 20 '15 at 20:10
  • @Tenzin, you cannot sort mixed types in python3 as you cannot sort string digits correctly in either python2 or 3. – Padraic Cunningham Dec 20 '15 at 20:12
  • Then how come Example 1 works in: http://www.tutorialspoint.com/execute_python3_online.php ? – Tenzin Dec 20 '15 at 20:13
  • @Tenzin, I don't need to check, I can guarantee you it does not work, I suggest you try doing it with numbers with more that 1 digit. Strings are compared lexicographically so single digit strings are not going to tell you much – Padraic Cunningham Dec 20 '15 at 20:15
  • If you use `sort()` with digits you should add `key=float`, for example: `listOne.sort(key=float)` Then it still will work. I just tested it. – Tenzin Dec 20 '15 at 20:19