这是我的整个代码段,但是在玩游戏后我很难使比分显示出来。分数已正确添加到文件中,但是当我打印分数时,即使我已经添加了新分数也没有出现。如何修复我的代码以使其显示?我是编码新手,所以对您有所帮助。我认为这是一个容易解决的错误,但我不知道如何解决。谢谢!
#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!")
答案 0 :(得分:1)
您永远不会更新scoreslist
使其包含sum
(通过append
对其进行编辑或再次从文件中读取scoreslist
)。添加将sum
添加到scoreslist
的行:
add_file_contents(scoreslist_path, '\n' + str(sum))
scoresline.append(sum)