Python Highscores w/ text file

2024/11/16 14:46:35

i am currently working on a text based game, and have run into a problem trying to make my last function, highscore. My problem is this, i would like to make the function save my top five scores and save it to a text file, along with the player's username, and then be able to print this into my text based game. I know this could be hard to do, but i would like it if the scores were saved in a text file, and not a pickle as i have seen suggested, as i would like to share the file with some friends. Any help is appreciated, Thanks, Tom.

edit: I am comfortable opening the file for writing. my code included the scores.sort command, but i wasn't sure how to put 5 scores into the file, and have the 6th score deleted. Thanks for the help, Tom

Answer

Welcome to Stack Overflow! What are you using to store your high scores in Python? A class, a dictionary?

To simply write string content to a file you can do the following:

scores = [('username1', 123), ('username2', 456)]
with open('scores.txt', 'w') as f:for username, score in scores:f.write('Username: {0}, Score: {1}\n'.format(username, score))
https://en.xdnf.cn/q/119186.html

Related Q&A

Do any one know about this Error in python? how can I resolve this?

I am plotting the data with MapBoxGl Python Library on maps, here is my code which is taking the latitude, longitude and points from the Pandas DataFrame and trying to make the geojson, here is the cod…

Pandas: Count Higher Ranks For Current Experiment Participants In Later Experiments (Part 1)

Learning Experiments In a series of learning experiments, I would like to count the number of participants in each experiment that improved their performance in subsequent experiments (Rank 1 is highes…

Pass variables into and out of exec [closed for not being clear. Modified and reuploaded] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

List Object Not Callable, Syntax Error for Text-Based RPG

Im having issues (still) with generating a random object, in this case a random herb found by foraging. Heres the code for the function:def collectPlants(self):if self.state == normal:print"%s spe…

Why does my Tkinter window background not change?

Ive a small program with a feature to change the background color of a different window than the frame I use to ask for the background color. (I hope you understand this.) The program is written in Pyt…

Combining a nested list without affecting the key and value direction in python

I have a program that stores data in a list. The current and desired output is in the format:# Current Input [{Devices: [laptops, tablets],ParentCategory: [computers, computers]},{Devices: [touch], Par…

Splitting very large csv files into smaller files

Is Dask proper to read large csv files in parallel and split them into multiple smaller files?

How to make this Python script to run subfolders too?

Which part of the codes do I need to change in order to include subfolders? File handle.py import glob import os import sys from typing import Listdef get_filenames(filepath: str, pattern: str) -> …

Why doesnt this recursive GCD function work? [duplicate]

This question already has answers here:Why does my recursive function return None?(4 answers)What is the purpose of the return statement? How is it different from printing?(15 answers)Closed 1 year …

How can I append \n at the end of the list in list comperhansion

I have this code:def table(h, w):table = [[. for i in range(w)] for j in range(h)]return tablewhich returns this[ [., ., .], [., ., .], [., ., .], [., ., .] ]How to make it return this?[[., ., .],[., …