Number is adding to file but not appearing when I print it
This is my entire code block but I am just having trouble making the scores show after the game is played. The score is being added to the file correctly, but when I go to print the scores, the new score is not appearing, even though I have appended it. How can I fix my code to make it show up? I am new to coding so anything helps. I think this is an easily solvable error, but I don't know how to fix it. Thank you!
#STAGE 1: Opening the files and grabbing data
users_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\usernames.txt"
passwords_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\passwords.txt"
scoreslist_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\scores.txt"
#Define functions for future use
def get_file_contents(file_path):
return [line.strip() for line in open(file_path)]
scoreslist = get_file_contents(scoreslist_path)
def add_file_contents(file_path, contents):
with open(file_path, "a") as file:
file.write(contents)
def login_user(new_account=False):
usernameslist = get_file_contents(users_path)
passwordslist = get_file_contents(passwords_path)
#Determine if user needs to create a new account:
if new_account:
response = 'y'
else:
response = input("-"*50 + "nWelcome! Do you have an account (y/n)? ")
print("-"*50)
#If user has an account:
if response == "y":
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print(print_in_green + "Access granted!" + print_default)
#Ask if user would like to view leaderboard
leaderboard = input("Would you like to view the leaderboard (y/n)? ")
#If thet want to see leaderboard:
if leaderboard == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK!")
#If they type the wrong username or password:
else:
print(print_in_red + "Incorrect Login credentials, please try again by restarting." + print_default)
#If user does not have account:
else:
goodlogin2 = False
newusername = input("What is your new username? ")
#Check to see if username already exists
if newusername in usernameslist:
print("This username is already taken. Please try another.")
else:
goodlogin2 = True
print(print_in_green + "Ok, please continue!" + print_default)
#Check to see if two passwords match
newpassword = input("What is your new password? ")
newpasswordagain = input("Please enter your new password again: ")
if newpassword == newpasswordagain:
print("Please follow the instructions to log in with your new credentials.")
add_file_contents(users_path, 'n' + newusername)
add_file_contents(passwords_path, 'n' + newpassword)
login_user(new_account=True)
else:
print(print_in_red + "Your passwords do not match. Please try again." + print_default)
login_user()
#Playing the game (rolling dice):
import random
min = 1
max = 6
sum = 0
#Ask user if they want to play
game_response = input("Would you like to play the game by rolling your dice (y/n)? ")
if game_response == "y":
roll_again = "yes"
while roll_again == "yes" or roll_again == "y":
print("Rolling the dices...")
print("The values are:")
dice1 = random.randint(min, max)
dice2 = random.randint(min, max)
print(print_in_pink)
print(int(dice1))
print(int(dice2))
print(print_default)
score = (int(dice1) + int(dice2))
sum = sum + score
roll_again = input("Your score for this round is " + str(score) + ". Roll the dices again (y/n)? ")
else:
print("Ok!")
print("Your final score is " + print_in_pink + str(sum) + print_default + "!")
add_file_contents(scoreslist_path, 'n' + str(sum))
scoreslist.append(str(sum))
leaderboard_again = input("Would you like to view the leaderboard again (y/n)? ")
if leaderboard_again == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK. Thanks for logging in!")
python
add a comment |
This is my entire code block but I am just having trouble making the scores show after the game is played. The score is being added to the file correctly, but when I go to print the scores, the new score is not appearing, even though I have appended it. How can I fix my code to make it show up? I am new to coding so anything helps. I think this is an easily solvable error, but I don't know how to fix it. Thank you!
#STAGE 1: Opening the files and grabbing data
users_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\usernames.txt"
passwords_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\passwords.txt"
scoreslist_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\scores.txt"
#Define functions for future use
def get_file_contents(file_path):
return [line.strip() for line in open(file_path)]
scoreslist = get_file_contents(scoreslist_path)
def add_file_contents(file_path, contents):
with open(file_path, "a") as file:
file.write(contents)
def login_user(new_account=False):
usernameslist = get_file_contents(users_path)
passwordslist = get_file_contents(passwords_path)
#Determine if user needs to create a new account:
if new_account:
response = 'y'
else:
response = input("-"*50 + "nWelcome! Do you have an account (y/n)? ")
print("-"*50)
#If user has an account:
if response == "y":
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print(print_in_green + "Access granted!" + print_default)
#Ask if user would like to view leaderboard
leaderboard = input("Would you like to view the leaderboard (y/n)? ")
#If thet want to see leaderboard:
if leaderboard == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK!")
#If they type the wrong username or password:
else:
print(print_in_red + "Incorrect Login credentials, please try again by restarting." + print_default)
#If user does not have account:
else:
goodlogin2 = False
newusername = input("What is your new username? ")
#Check to see if username already exists
if newusername in usernameslist:
print("This username is already taken. Please try another.")
else:
goodlogin2 = True
print(print_in_green + "Ok, please continue!" + print_default)
#Check to see if two passwords match
newpassword = input("What is your new password? ")
newpasswordagain = input("Please enter your new password again: ")
if newpassword == newpasswordagain:
print("Please follow the instructions to log in with your new credentials.")
add_file_contents(users_path, 'n' + newusername)
add_file_contents(passwords_path, 'n' + newpassword)
login_user(new_account=True)
else:
print(print_in_red + "Your passwords do not match. Please try again." + print_default)
login_user()
#Playing the game (rolling dice):
import random
min = 1
max = 6
sum = 0
#Ask user if they want to play
game_response = input("Would you like to play the game by rolling your dice (y/n)? ")
if game_response == "y":
roll_again = "yes"
while roll_again == "yes" or roll_again == "y":
print("Rolling the dices...")
print("The values are:")
dice1 = random.randint(min, max)
dice2 = random.randint(min, max)
print(print_in_pink)
print(int(dice1))
print(int(dice2))
print(print_default)
score = (int(dice1) + int(dice2))
sum = sum + score
roll_again = input("Your score for this round is " + str(score) + ". Roll the dices again (y/n)? ")
else:
print("Ok!")
print("Your final score is " + print_in_pink + str(sum) + print_default + "!")
add_file_contents(scoreslist_path, 'n' + str(sum))
scoreslist.append(str(sum))
leaderboard_again = input("Would you like to view the leaderboard again (y/n)? ")
if leaderboard_again == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK. Thanks for logging in!")
python
1
Issum
inscoreslist
? Is your assignment asking you to implement your own sort here? If so, you should be factoring the sorting out into its own function. Otherwise, you should be using the built-inlist.sort
. Could you provide some sample values of the variables this code doesn't define, so that we can have a Minimal, Complete, and Verifiable example that demonstrates the problem?
– Patrick Haugh
Nov 19 '18 at 18:28
I updated it with all of my code so hopefully it's more clear now. Did that help?
– Anna Hamelin
Nov 19 '18 at 19:11
add a comment |
This is my entire code block but I am just having trouble making the scores show after the game is played. The score is being added to the file correctly, but when I go to print the scores, the new score is not appearing, even though I have appended it. How can I fix my code to make it show up? I am new to coding so anything helps. I think this is an easily solvable error, but I don't know how to fix it. Thank you!
#STAGE 1: Opening the files and grabbing data
users_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\usernames.txt"
passwords_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\passwords.txt"
scoreslist_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\scores.txt"
#Define functions for future use
def get_file_contents(file_path):
return [line.strip() for line in open(file_path)]
scoreslist = get_file_contents(scoreslist_path)
def add_file_contents(file_path, contents):
with open(file_path, "a") as file:
file.write(contents)
def login_user(new_account=False):
usernameslist = get_file_contents(users_path)
passwordslist = get_file_contents(passwords_path)
#Determine if user needs to create a new account:
if new_account:
response = 'y'
else:
response = input("-"*50 + "nWelcome! Do you have an account (y/n)? ")
print("-"*50)
#If user has an account:
if response == "y":
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print(print_in_green + "Access granted!" + print_default)
#Ask if user would like to view leaderboard
leaderboard = input("Would you like to view the leaderboard (y/n)? ")
#If thet want to see leaderboard:
if leaderboard == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK!")
#If they type the wrong username or password:
else:
print(print_in_red + "Incorrect Login credentials, please try again by restarting." + print_default)
#If user does not have account:
else:
goodlogin2 = False
newusername = input("What is your new username? ")
#Check to see if username already exists
if newusername in usernameslist:
print("This username is already taken. Please try another.")
else:
goodlogin2 = True
print(print_in_green + "Ok, please continue!" + print_default)
#Check to see if two passwords match
newpassword = input("What is your new password? ")
newpasswordagain = input("Please enter your new password again: ")
if newpassword == newpasswordagain:
print("Please follow the instructions to log in with your new credentials.")
add_file_contents(users_path, 'n' + newusername)
add_file_contents(passwords_path, 'n' + newpassword)
login_user(new_account=True)
else:
print(print_in_red + "Your passwords do not match. Please try again." + print_default)
login_user()
#Playing the game (rolling dice):
import random
min = 1
max = 6
sum = 0
#Ask user if they want to play
game_response = input("Would you like to play the game by rolling your dice (y/n)? ")
if game_response == "y":
roll_again = "yes"
while roll_again == "yes" or roll_again == "y":
print("Rolling the dices...")
print("The values are:")
dice1 = random.randint(min, max)
dice2 = random.randint(min, max)
print(print_in_pink)
print(int(dice1))
print(int(dice2))
print(print_default)
score = (int(dice1) + int(dice2))
sum = sum + score
roll_again = input("Your score for this round is " + str(score) + ". Roll the dices again (y/n)? ")
else:
print("Ok!")
print("Your final score is " + print_in_pink + str(sum) + print_default + "!")
add_file_contents(scoreslist_path, 'n' + str(sum))
scoreslist.append(str(sum))
leaderboard_again = input("Would you like to view the leaderboard again (y/n)? ")
if leaderboard_again == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK. Thanks for logging in!")
python
This is my entire code block but I am just having trouble making the scores show after the game is played. The score is being added to the file correctly, but when I go to print the scores, the new score is not appearing, even though I have appended it. How can I fix my code to make it show up? I am new to coding so anything helps. I think this is an easily solvable error, but I don't know how to fix it. Thank you!
#STAGE 1: Opening the files and grabbing data
users_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\usernames.txt"
passwords_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\passwords.txt"
scoreslist_path = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\scores.txt"
#Define functions for future use
def get_file_contents(file_path):
return [line.strip() for line in open(file_path)]
scoreslist = get_file_contents(scoreslist_path)
def add_file_contents(file_path, contents):
with open(file_path, "a") as file:
file.write(contents)
def login_user(new_account=False):
usernameslist = get_file_contents(users_path)
passwordslist = get_file_contents(passwords_path)
#Determine if user needs to create a new account:
if new_account:
response = 'y'
else:
response = input("-"*50 + "nWelcome! Do you have an account (y/n)? ")
print("-"*50)
#If user has an account:
if response == "y":
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print(print_in_green + "Access granted!" + print_default)
#Ask if user would like to view leaderboard
leaderboard = input("Would you like to view the leaderboard (y/n)? ")
#If thet want to see leaderboard:
if leaderboard == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK!")
#If they type the wrong username or password:
else:
print(print_in_red + "Incorrect Login credentials, please try again by restarting." + print_default)
#If user does not have account:
else:
goodlogin2 = False
newusername = input("What is your new username? ")
#Check to see if username already exists
if newusername in usernameslist:
print("This username is already taken. Please try another.")
else:
goodlogin2 = True
print(print_in_green + "Ok, please continue!" + print_default)
#Check to see if two passwords match
newpassword = input("What is your new password? ")
newpasswordagain = input("Please enter your new password again: ")
if newpassword == newpasswordagain:
print("Please follow the instructions to log in with your new credentials.")
add_file_contents(users_path, 'n' + newusername)
add_file_contents(passwords_path, 'n' + newpassword)
login_user(new_account=True)
else:
print(print_in_red + "Your passwords do not match. Please try again." + print_default)
login_user()
#Playing the game (rolling dice):
import random
min = 1
max = 6
sum = 0
#Ask user if they want to play
game_response = input("Would you like to play the game by rolling your dice (y/n)? ")
if game_response == "y":
roll_again = "yes"
while roll_again == "yes" or roll_again == "y":
print("Rolling the dices...")
print("The values are:")
dice1 = random.randint(min, max)
dice2 = random.randint(min, max)
print(print_in_pink)
print(int(dice1))
print(int(dice2))
print(print_default)
score = (int(dice1) + int(dice2))
sum = sum + score
roll_again = input("Your score for this round is " + str(score) + ". Roll the dices again (y/n)? ")
else:
print("Ok!")
print("Your final score is " + print_in_pink + str(sum) + print_default + "!")
add_file_contents(scoreslist_path, 'n' + str(sum))
scoreslist.append(str(sum))
leaderboard_again = input("Would you like to view the leaderboard again (y/n)? ")
if leaderboard_again == "y":
print("-"*50 + "n" + print_in_blue + "Here is the leaderboard!n" + print_default + "-"*50)
for c in range(0, len(scoreslist)-1):
max = scoreslist[c]
index_of_max = c
for i in range (c+1, len(scoreslist)):
if (scoreslist[i] > max):
max = scoreslist[i]
index_of_max = i
aux = scoreslist[c]
scoreslist[c] = max
scoreslist[index_of_max] = aux
#print(scoreslist)
print(*scoreslist, sep = "n")
print("-"*50)
#If they don't want to see scores:
else:
print("OK. Thanks for logging in!")
python
python
edited Nov 19 '18 at 19:35
Anna Hamelin
asked Nov 19 '18 at 18:17
Anna HamelinAnna Hamelin
197
197
1
Issum
inscoreslist
? Is your assignment asking you to implement your own sort here? If so, you should be factoring the sorting out into its own function. Otherwise, you should be using the built-inlist.sort
. Could you provide some sample values of the variables this code doesn't define, so that we can have a Minimal, Complete, and Verifiable example that demonstrates the problem?
– Patrick Haugh
Nov 19 '18 at 18:28
I updated it with all of my code so hopefully it's more clear now. Did that help?
– Anna Hamelin
Nov 19 '18 at 19:11
add a comment |
1
Issum
inscoreslist
? Is your assignment asking you to implement your own sort here? If so, you should be factoring the sorting out into its own function. Otherwise, you should be using the built-inlist.sort
. Could you provide some sample values of the variables this code doesn't define, so that we can have a Minimal, Complete, and Verifiable example that demonstrates the problem?
– Patrick Haugh
Nov 19 '18 at 18:28
I updated it with all of my code so hopefully it's more clear now. Did that help?
– Anna Hamelin
Nov 19 '18 at 19:11
1
1
Is
sum
in scoreslist
? Is your assignment asking you to implement your own sort here? If so, you should be factoring the sorting out into its own function. Otherwise, you should be using the built-in list.sort
. Could you provide some sample values of the variables this code doesn't define, so that we can have a Minimal, Complete, and Verifiable example that demonstrates the problem?– Patrick Haugh
Nov 19 '18 at 18:28
Is
sum
in scoreslist
? Is your assignment asking you to implement your own sort here? If so, you should be factoring the sorting out into its own function. Otherwise, you should be using the built-in list.sort
. Could you provide some sample values of the variables this code doesn't define, so that we can have a Minimal, Complete, and Verifiable example that demonstrates the problem?– Patrick Haugh
Nov 19 '18 at 18:28
I updated it with all of my code so hopefully it's more clear now. Did that help?
– Anna Hamelin
Nov 19 '18 at 19:11
I updated it with all of my code so hopefully it's more clear now. Did that help?
– Anna Hamelin
Nov 19 '18 at 19:11
add a comment |
1 Answer
1
active
oldest
votes
You never update scoreslist
to have sum
in it (either by append
ing it or by reading scoreslist
out of the file again). Add a line that adds sum
to scoreslist
:
add_file_contents(scoreslist_path, 'n' + str(sum))
scoresline.append(sum)
That worked but now I have the error "TypeError: '>' not supported between instances of 'int' and 'str'" in line 148. How can I fix that?
– Anna Hamelin
Nov 19 '18 at 19:21
1
You would appendstr(sum)
instead ofsum
. Note that when you sort strings, they're sorted lexicographically, so2 > 10
. It would make more sense to have everything inscoreslist
be an integer by changingget_file_contents
to[int(line.strip()) for line in open(file_path)]
. There may be other changes you would need to support that though.
– Patrick Haugh
Nov 19 '18 at 19:24
I made some changes and updated my code but the new score still isn't appearing when I print the leaderboard after the game. Any ideas?
– Anna Hamelin
Nov 19 '18 at 19:37
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%2f53380496%2fnumber-is-adding-to-file-but-not-appearing-when-i-print-it%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
You never update scoreslist
to have sum
in it (either by append
ing it or by reading scoreslist
out of the file again). Add a line that adds sum
to scoreslist
:
add_file_contents(scoreslist_path, 'n' + str(sum))
scoresline.append(sum)
That worked but now I have the error "TypeError: '>' not supported between instances of 'int' and 'str'" in line 148. How can I fix that?
– Anna Hamelin
Nov 19 '18 at 19:21
1
You would appendstr(sum)
instead ofsum
. Note that when you sort strings, they're sorted lexicographically, so2 > 10
. It would make more sense to have everything inscoreslist
be an integer by changingget_file_contents
to[int(line.strip()) for line in open(file_path)]
. There may be other changes you would need to support that though.
– Patrick Haugh
Nov 19 '18 at 19:24
I made some changes and updated my code but the new score still isn't appearing when I print the leaderboard after the game. Any ideas?
– Anna Hamelin
Nov 19 '18 at 19:37
add a comment |
You never update scoreslist
to have sum
in it (either by append
ing it or by reading scoreslist
out of the file again). Add a line that adds sum
to scoreslist
:
add_file_contents(scoreslist_path, 'n' + str(sum))
scoresline.append(sum)
That worked but now I have the error "TypeError: '>' not supported between instances of 'int' and 'str'" in line 148. How can I fix that?
– Anna Hamelin
Nov 19 '18 at 19:21
1
You would appendstr(sum)
instead ofsum
. Note that when you sort strings, they're sorted lexicographically, so2 > 10
. It would make more sense to have everything inscoreslist
be an integer by changingget_file_contents
to[int(line.strip()) for line in open(file_path)]
. There may be other changes you would need to support that though.
– Patrick Haugh
Nov 19 '18 at 19:24
I made some changes and updated my code but the new score still isn't appearing when I print the leaderboard after the game. Any ideas?
– Anna Hamelin
Nov 19 '18 at 19:37
add a comment |
You never update scoreslist
to have sum
in it (either by append
ing it or by reading scoreslist
out of the file again). Add a line that adds sum
to scoreslist
:
add_file_contents(scoreslist_path, 'n' + str(sum))
scoresline.append(sum)
You never update scoreslist
to have sum
in it (either by append
ing it or by reading scoreslist
out of the file again). Add a line that adds sum
to scoreslist
:
add_file_contents(scoreslist_path, 'n' + str(sum))
scoresline.append(sum)
answered Nov 19 '18 at 19:13
Patrick HaughPatrick Haugh
28.6k82747
28.6k82747
That worked but now I have the error "TypeError: '>' not supported between instances of 'int' and 'str'" in line 148. How can I fix that?
– Anna Hamelin
Nov 19 '18 at 19:21
1
You would appendstr(sum)
instead ofsum
. Note that when you sort strings, they're sorted lexicographically, so2 > 10
. It would make more sense to have everything inscoreslist
be an integer by changingget_file_contents
to[int(line.strip()) for line in open(file_path)]
. There may be other changes you would need to support that though.
– Patrick Haugh
Nov 19 '18 at 19:24
I made some changes and updated my code but the new score still isn't appearing when I print the leaderboard after the game. Any ideas?
– Anna Hamelin
Nov 19 '18 at 19:37
add a comment |
That worked but now I have the error "TypeError: '>' not supported between instances of 'int' and 'str'" in line 148. How can I fix that?
– Anna Hamelin
Nov 19 '18 at 19:21
1
You would appendstr(sum)
instead ofsum
. Note that when you sort strings, they're sorted lexicographically, so2 > 10
. It would make more sense to have everything inscoreslist
be an integer by changingget_file_contents
to[int(line.strip()) for line in open(file_path)]
. There may be other changes you would need to support that though.
– Patrick Haugh
Nov 19 '18 at 19:24
I made some changes and updated my code but the new score still isn't appearing when I print the leaderboard after the game. Any ideas?
– Anna Hamelin
Nov 19 '18 at 19:37
That worked but now I have the error "TypeError: '>' not supported between instances of 'int' and 'str'" in line 148. How can I fix that?
– Anna Hamelin
Nov 19 '18 at 19:21
That worked but now I have the error "TypeError: '>' not supported between instances of 'int' and 'str'" in line 148. How can I fix that?
– Anna Hamelin
Nov 19 '18 at 19:21
1
1
You would append
str(sum)
instead of sum
. Note that when you sort strings, they're sorted lexicographically, so 2 > 10
. It would make more sense to have everything in scoreslist
be an integer by changing get_file_contents
to [int(line.strip()) for line in open(file_path)]
. There may be other changes you would need to support that though.– Patrick Haugh
Nov 19 '18 at 19:24
You would append
str(sum)
instead of sum
. Note that when you sort strings, they're sorted lexicographically, so 2 > 10
. It would make more sense to have everything in scoreslist
be an integer by changing get_file_contents
to [int(line.strip()) for line in open(file_path)]
. There may be other changes you would need to support that though.– Patrick Haugh
Nov 19 '18 at 19:24
I made some changes and updated my code but the new score still isn't appearing when I print the leaderboard after the game. Any ideas?
– Anna Hamelin
Nov 19 '18 at 19:37
I made some changes and updated my code but the new score still isn't appearing when I print the leaderboard after the game. Any ideas?
– Anna Hamelin
Nov 19 '18 at 19:37
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%2f53380496%2fnumber-is-adding-to-file-but-not-appearing-when-i-print-it%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
1
Is
sum
inscoreslist
? Is your assignment asking you to implement your own sort here? If so, you should be factoring the sorting out into its own function. Otherwise, you should be using the built-inlist.sort
. Could you provide some sample values of the variables this code doesn't define, so that we can have a Minimal, Complete, and Verifiable example that demonstrates the problem?– Patrick Haugh
Nov 19 '18 at 18:28
I updated it with all of my code so hopefully it's more clear now. Did that help?
– Anna Hamelin
Nov 19 '18 at 19:11