Python Highscores w/ text file
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
python python-3.x xcode
add a comment |
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
python python-3.x xcode
It is not too hard to do; you just need to break the task down into steps and search your way through them. Have you opened the file for writing? Please include an MCVE to show where you are up to and try to be specific about what the issue is that you're struggling with.
– roganjosh
Nov 14 '18 at 11:21
add a comment |
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
python python-3.x xcode
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
python python-3.x xcode
python python-3.x xcode
edited Nov 16 '18 at 9:23
Tom
asked Nov 14 '18 at 11:19
TomTom
113
113
It is not too hard to do; you just need to break the task down into steps and search your way through them. Have you opened the file for writing? Please include an MCVE to show where you are up to and try to be specific about what the issue is that you're struggling with.
– roganjosh
Nov 14 '18 at 11:21
add a comment |
It is not too hard to do; you just need to break the task down into steps and search your way through them. Have you opened the file for writing? Please include an MCVE to show where you are up to and try to be specific about what the issue is that you're struggling with.
– roganjosh
Nov 14 '18 at 11:21
It is not too hard to do; you just need to break the task down into steps and search your way through them. Have you opened the file for writing? Please include an MCVE to show where you are up to and try to be specific about what the issue is that you're struggling with.
– roganjosh
Nov 14 '18 at 11:21
It is not too hard to do; you just need to break the task down into steps and search your way through them. Have you opened the file for writing? Please include an MCVE to show where you are up to and try to be specific about what the issue is that you're struggling with.
– roganjosh
Nov 14 '18 at 11:21
add a comment |
1 Answer
1
active
oldest
votes
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))
edit: The code works absolute wonders, thanks! Just a question, the new score seems to replace the old one even if it is lower. For example, i have a score of 15 and then get a score of 0, the file looks like thisUsername: tom, Score: 0
– Tom
Nov 16 '18 at 9:33
Do i need to open the file first and check if the new score is larger than the old one, and if so, how would i go about doing this?
– Tom
Nov 16 '18 at 9:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299004%2fpython-highscores-w-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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))
edit: The code works absolute wonders, thanks! Just a question, the new score seems to replace the old one even if it is lower. For example, i have a score of 15 and then get a score of 0, the file looks like thisUsername: tom, Score: 0
– Tom
Nov 16 '18 at 9:33
Do i need to open the file first and check if the new score is larger than the old one, and if so, how would i go about doing this?
– Tom
Nov 16 '18 at 9:47
add a comment |
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))
edit: The code works absolute wonders, thanks! Just a question, the new score seems to replace the old one even if it is lower. For example, i have a score of 15 and then get a score of 0, the file looks like thisUsername: tom, Score: 0
– Tom
Nov 16 '18 at 9:33
Do i need to open the file first and check if the new score is larger than the old one, and if so, how would i go about doing this?
– Tom
Nov 16 '18 at 9:47
add a comment |
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))
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))
edited Nov 14 '18 at 14:52
answered Nov 14 '18 at 11:26
Graham HealyGraham Healy
515
515
edit: The code works absolute wonders, thanks! Just a question, the new score seems to replace the old one even if it is lower. For example, i have a score of 15 and then get a score of 0, the file looks like thisUsername: tom, Score: 0
– Tom
Nov 16 '18 at 9:33
Do i need to open the file first and check if the new score is larger than the old one, and if so, how would i go about doing this?
– Tom
Nov 16 '18 at 9:47
add a comment |
edit: The code works absolute wonders, thanks! Just a question, the new score seems to replace the old one even if it is lower. For example, i have a score of 15 and then get a score of 0, the file looks like thisUsername: tom, Score: 0
– Tom
Nov 16 '18 at 9:33
Do i need to open the file first and check if the new score is larger than the old one, and if so, how would i go about doing this?
– Tom
Nov 16 '18 at 9:47
edit: The code works absolute wonders, thanks! Just a question, the new score seems to replace the old one even if it is lower. For example, i have a score of 15 and then get a score of 0, the file looks like this
Username: tom, Score: 0
– Tom
Nov 16 '18 at 9:33
edit: The code works absolute wonders, thanks! Just a question, the new score seems to replace the old one even if it is lower. For example, i have a score of 15 and then get a score of 0, the file looks like this
Username: tom, Score: 0
– Tom
Nov 16 '18 at 9:33
Do i need to open the file first and check if the new score is larger than the old one, and if so, how would i go about doing this?
– Tom
Nov 16 '18 at 9:47
Do i need to open the file first and check if the new score is larger than the old one, and if so, how would i go about doing this?
– Tom
Nov 16 '18 at 9:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299004%2fpython-highscores-w-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It is not too hard to do; you just need to break the task down into steps and search your way through them. Have you opened the file for writing? Please include an MCVE to show where you are up to and try to be specific about what the issue is that you're struggling with.
– roganjosh
Nov 14 '18 at 11:21