Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving to a CSV
#1
Hi,

I have this section fo code that is shown below.

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
    print (username_2 + " got the cards: ") #tells you what cards they got
    print (player_2_cards) #shows the cards
    with open("Card Game Scores.csv", "a+", newline = "") as save_score_1: #looking at what is in the file  
        save_score_1.write(player_1_results) #writes in the file what the users results were
        save_score_1.write("\n") #adds a space to make it clearer
        save_score_1.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
    print (username_1 + " got the cards: ") #tells you what cards they got
    print (player_1_cards) #shows the cards
    with open("Card Game Scores.csv", "a+", newline = "") as save_score_2: #looking at what is in the file  
        save_score_2.write(player_2_results) #writes in the file what the users results were
        save_score_2.write("\n") #adds a space to make it clearer
        save_score_2.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 ("These are the top 5 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") #opens the file where scores are saved
card_csv = csv.reader(all_scores,delimiter=",") #reads what is in the file, delimiter sets it so that the comma is the seperator
sort_csv = sorted(card_csv,key=lambda elem: str(elem[1]),reverse=True) #reverses it to make it highest to lowest, lambda allows it to be sorted correctly
for count,eachline in enumerate(sort_csv): #for loop to print the scores
    if count<5: #loops until it is greater than 5
       print (eachline) #prints eachline of the scores
what I want to happen is whoever has won it saves there score to the CSV file. however, what is happening is it is only saving the first players scores to the CSV no matter if they win or lose. I want the winners score to be saved and only there score.

the output just outputs whos won and then the top 5 previous scores. This top 5 is now inaccurate because it is saving only player 1s score and name and not anyone else's.

In conclusion I want the winners name and score to be saved to the CSV

thanks

Since i have posted this i have figured out the solution. it was a simple error on my side where I told it to save player 1s results when I wanted player 2s

I didn't show this code in the previous post. my fault

previous:

player_1_results = str([username_1, amount_of_player_1_cards]) #combines the username and the amount of cards they have
player_2_results = str([username_1, amount_of_player_1_cards]) #combines the username and the amount of cards they have
changed to:

player_1_results = str([username_1, amount_of_player_1_cards]) #combines the username and the amount of cards they have
player_2_results = str([username_2, amount_of_player_2_cards]) #combines the username and the amount of cards they have
Reply
#2
Can you post sample input and expected output?
Reply
#3
as before mentioned i have found a solutuon.
Reply


Forum Jump:

User Panel Messages

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