Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
George Jenkins
107 PointsHow do i get my code to order data This is for my computing coursework so any help would be really appreciated :)thanks
import random #Random Function import os
print("Enter your name?") #Asking User Name name = input().title() #.title() capitilises the first letter in each word, so when the name is printed in later code the first letter will always be capitilised. cname = input ("Which class do you wish to input results for 1 2 or 3?") #Finding out the users class. print ("The test will now begin. Do your best!") score = 0 #Score starts at 0 question = 0 #Set Question Number to 0 while question < 10: #Means the program will stop after 10 questions operator = random.randint(1,3)#Addition,Multiplication,Subtraction num1 = random.randint(1,10)#Random Number between 1 and 10 to be used in the questions num2 = random.randint(1,10)#Random Number between 1 and 10 to be used in the questions if operator == 1: #If Random Operator = 1, it will ask addition questions print("What is", num1, "+", num2) ans = num1 + num2
elif operator == 2:#If Random Operator = 2, it will ask multiplication questions
print("What is", num1, "*", num2)
ans = num1 * num2
else: #If Random Operator = 3/Else, it will ask subtraction questions.
print("What is", num1, "-", num2)
ans = num1 - num2
while True:
try:
user_ans = int(input()) #The Answer the user inputs to the question asked
except ValueError: #When anything but a interger is inputted.
print ("That is not a valid answer")
continue #Dosen't print Invalid Code Error
else:
break
if user_ans == ans: #If user answer matches that of the question
print("Correct")
score += 1
else: #If user answer dosen't match that of a question.
print("Incorrect")
question += 1 #When a question is answered, it will +1 until it equals 10 at which point it will stop.
print("Well done", name, "you scored", score, "/10") #Print Final Score out of 10
cname = cname + ".txt" #adds '.txt' to the end of the file so it can be used to create a file under the name a user specifies
file = open(cname , 'a') #opens the file in 'append' mode so you don't delete all the information name = (name) file.write(str(name + " : " )) #writes the information to the file file.write(str(score)) file.write('\n') file.close() #safely closes the file to save the information
view_score = input("Do you want to view other results from your class?").lower() #asking if they want to see other results
if viewscore == "yes".lower(): f = open(cname, 'r') file_contents = f.read() print (file_contents) f.close() #Shows them the results of them and their class mates
elif view_score != "no".lower(): print ("Press any key to exit") ex = input ("") os._exit(0)
sort_score = input ("Do you want to sort the score?") if sort_score == "yes".lower():
if cname == 1:
print('Enter your score for test 2')
test2 = int(input())
print('Enter your score for test 3')
test3 = int(input())
with open('class1.csv', 'a', newline='') as fp:
a = csv.writer(fp,delimiter=',')
data=[[name,score,test2,test3]]
a.writerows(data)
print("Your score has been saved")
f = open('class1.csv')
csv_f = csv.reader(f)
newlist = []
for row in csv_f:
row[1] = int(row[1])
row[2] = int(row[2])
row[3] = int(row[3])
minimum = min(row[1:4])
row.append(minimum)
maximum = max(row[1:4])
row.append(maximum)
average = round(sum(row[1:4])/3)
row.append(average)
newlist.append(row[0:7])
alphabetical = [[x[0], x[5]] for x in newlist]
for entry in sorted(alphabetical):
print(entry)
Highscore = [[x[5], x[0]]for x in newlist]
print('Highest scores')
for entry in sorted(Highscore, reverse = True) :
print(entry)
averagescore = [[x[6], x[0]] for x in newlist]
print('Average scores')
for entry in sorted(averagescore, reverse = True) :
print(entry)
if cname == 2:
print('Enter your score for test 2')
test2 = int(input())
print('Enter your score for test 3')
test3 = int(input())
with open('class8.csv','a',newline='') as fp:
a = csv.writer(fp,delimiter=',')
data=[[name,score,test2,test3]]
a.writerows(data)
print("Your score has been saved")
f = open('class8.csv')
csv_f = csv.reader(f)
newlist = []
for row in csv_f:
row[1] = int(row[1])
row[2] = int(row[2])
row[3] = int(row[3])
minimum = min(row[1:4])
row.append(minimum)
maximum = max(row[1:4])
row.append(maximum)
average = round(sum(row[1:4])/3)
row.append(average)
newlist.append(row[0:7])
alphabetical = [[x[0], x[5]] for x in newlist]
for entry in sorted(alphabetical):
print(entry)
Highscore = [[x[5], x[0]]for x in newlist]
print('Highest scores')
for entry in sorted(Highscore, reverse = True) :
print(entry)
averagescore = [[x[6], x[0]] for x in newlist]
print('Average scores')
for entry in sorted(averagescore, reverse = True) :
print(entry)
if cname == 3:
print('Enter your score for test 2')
test2 = int(input())
print('Enter your score for test 3')
test3 = int(input())
with open('class9.csv','a',newline='') as fp:
a = csv.writer(fp, delimiter=',')
data=[[name,score,test2,test3]]
a.writerows(data)
print("Your score has been saved")
f = open('class9.csv')
csv_f = csv.reader(f)
newlist = []
for row in csv_f:
row[1] = int(row[1])
row[2] = int(row[2])
row[3] = int(row[3])
minimum = min(row[1:4])
row.append(minimum)
maximum = max(row[1:4])
row.append(maximum)
average = round(sum(row[1:4])/3)
row.append(average)
newlist.append(row[0:7])
alphabetical = [[x[0], x[5]] for x in newlist]
for entry in sorted(alphabetical):
print(entry)
Highscore = [[x[5], x[0]]for x in newlist]
print('Highest scores')
for entry in sorted(Highscore, reverse = True) :
print(entry)
averagescore = [[x[6], x[0]] for x in newlist]
print('Average scores')
for entry in sorted(averagescore, reverse = True) :
print(entry)
1 Answer
Dan Oswalt
23,438 PointsHi George,
It's not clear what you're specifically asking about, but lists can be sorted very easily with python's built in .sort method for lists if that's all you need to do:
number_list = [2, 3, 4, 8, 1, 7, 6]
alpha_list = ['c', 'b', 'e', 'a', 'd']
number_list.sort()
alpha_list.sort()
alpha_list.reverse()
#[1, 2, 3, 4, 6, 7, 8]
#['e', 'd', 'c', 'b', 'a']
George Jenkins
107 PointsGeorge Jenkins
107 PointsThank you, sorry the code came out really but whats supposed to happen is i take the data from the students maths quiz and then am able to order them alphabetically, highest to lowest and by average highest to lowest, however the code doesnt go on to that bit and im getting some errors.
The code youve given me wouldn't really work i dont think because i dont know the numbers yet. I dont put what the students get into my code. Is there a way to make that preset so maybe a variable = their score and then it orders their score by those variables?