Python Forum
Need Help With A Problem I Have Come Across
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help With A Problem I Have Come Across
#11
Hi,

I only want to display the first five rows and I also want to sort it from highest to lowest.
not sure what to do so came here.

print ("Time for the results!!!") #prints time for results
time.sleep(1) #adds a one second break
if amount_of_player_1_cards > amount_of_player_2_cards: #if the amount of cards player one has is bigger than the amount player two has
    print ("The winner, with " + amount_of_player_1_cards + " is....... " + username_1) #tells you the winner
    time.sleep(1) #adds a one seocnd break
    print (username_1 + " got the cards: ") #tells you what cards they got
    print (player_1_cards) #shows the cards
    time.sleep(1) #adds a one second break
    print ("That makes " + username_2 + " second place with " + amount_of_player_2_cards + " cards") #tells you who came 2nd
    with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file  
        save_score.write(player_1_results) #writes in the file what the users results were
        save_score.write("\n") #adds a space to make it clearer
        save_score.close() #saves and closes the file
else:
    print ("The winner, with " + amount_of_player_2_cards + " is....... " + username_2) #tells you the winner
    time.sleep(1) #adds a one second break
    print (username_2 + " got the cards: ") #tells you what cards they got
    print (player_2_cards) #shows the cards
    time.sleep(2) #adds a one second break
    print ("That makes " + username_1 + " second place with " + amount_of_player_1_cards + " cards") #tells you who came 2nd
    with open("Card Game Scores.csv", "a+", newline = "") as save_score: #looking at what is in the file  
        save_score.write(player_2_results) #writes in the file what the users results were
        save_score.write("\n") #adds a space to make it clearer
        save_score.close() #saves and closes the file
   

#####################################################################################################
time.sleep(2) #adds a 2 second break
print ("This is how your scores compare to other winners of this game: ") #tells you this is how your socres compare to others
time.sleep(1) #adds a 2 second break
print ("This is these players most recent scores:") #tells you these are there most recent scores
time.sleep(1) #adds a 1 second break
all_scores = open("Card Game Scores.csv", "r")
card_csv = csv.reader(all_scores,delimiter=",")
sort_csv = sorted(card_csv,key=operator.itemgetter(1))
for eachline in sort_csv:
    print (eachline)
this is the output:

Output:
Time for the results!!! The winner, with 20 is....... finndude finndude got the cards: ['black:10', 'black:07', 'red:08', 'black:02', 'red:09', 'black:06', 'black:04', 'yellow:01', 'red:05', 'black:01', 'yellow:06', 'red:10', 'black:09', 'yellow:08', 'black:08', 'black:03', 'yellow:04', 'red:02', 'red:07', 'black:05'] That makes player2 second place with 10 cards This is how your scores compare to other winners of this game: This is these players most recent scores: ["['finndude'", " '10']"] ["['finndude'", " '12']"] ["['finndude'", " '14']"] ["['finndude'", " '14']"] ["['finndude'", " '16']"] ["['finndude'", " '16']"] ["['player1'", " '18']"] ["['finndude'", " '20']"] ["['player1'", " '22']"] ["['finndude'", " '8']"]
the list of values is all stored in a CSV file

I want the output to be highest to lowest and only show the top 5 scores.

thanks.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020