【发布时间】:2020-12-18 05:18:08
【问题描述】:
我正在为一个学校项目做一个音乐测验。
我已经制作了一个可以运行的游戏,但是我无法让排行榜(应该是文本并保存为 leaderboard.txt)显示不同的名称,因为它会覆盖以前的名称。
例如,如果“Sam”得 9 分,“Ben”得 3 分,它会显示为“Ben-3-9”,这不是我想要的。
我正在尝试让我的排行榜像这样工作:
Sam - 9
Ben - 3
...
我的代码现在看起来像这样:
username = input("What is your username?")
# this will ask for the persons name
password = str(input("What is the password?"))
# this will ask for a password which has been set already
if password == "1234":
print("User Authenticated")
# if the password is incorrect, tell the user so and exit
elif password != "1234":
print("Password Denied")
exit()
# GAME
# Creating a score variable
score=0
x = 0
# Reading song names and artist from the file
read = open("songnames.txt", "r")
songs = read.readlines()
songlist = []
# Removing the 'new line' code
for i in range(len(songs)):
songlist.append(songs[i].strip('\n'))
while x == 0:
# Randomly choosing a song and artist from the list
import random
choice = random.choice(songlist)
artist, song = choice.split('-')
# Splitting the song into the first letters of each word
songs = song.split()
letters = [word[0] for word in songs]
# Loop for guessing the answer
for x in range(0, 2):
print(artist, "".join(letters))
guess = str(input("Guess the song!"))
if guess == song:
if x == 0:
score = score + 3
break
if x == 1:
score = score + 1
break
quit()
# Printing score, Then waiting to start loop again.
import time
print("Your score is", score)
print("Nice Work!")
time.sleep(3)
leaderboard = open("leaderboard.txt", "r+")
leaderboard.write(username + '-' + '{}'.format(score))
leaderboard.close()
leaderboard = open("leaderboard.txt", "r+")
leaderboardlist = leaderboard.readlines()
print(leaderboardlist)
leaderboard.close()
PS:这不是 100% 我的代码我正试图从不同的地方获得帮助,因为由于大流行关闭学校,我的学校还没有教我们如何编码。
【问题讨论】:
标签: python leaderboard