我需要有关代码的帮助,该代码可以为用户尝试的每一次猜测计算分数。如果用户从第一次尝试中得到正确的答案,它将是3分,如果从第二次猜测中得到的是1分。当用户输赢时,它将用户名和得分存储在外部文件中。当所有用户参加比赛后,它将显示外部文件中排名前5位的获胜者。
我已经完成了要求用户输入用户名和密码并将其存储在外部设备中的部分。我还写下了一个代码,以显示歌手姓名和歌曲的首字母,并为用户提供2次尝试。
username= input("Please enter your username")
password= input("Please enter your password")
f=open("usernamepassword.txt","a")
f.write(username)
f.write(" ")
f.write(password)
f.write("\n")
f.close()
import random
for x in range(0, 1):
randNum = int(random.randint(0, 1))
song = open("Songs.txt", "r")
songname = str(song.readlines()[0])
print(songname[0])
song.close()
artist = open("Artists.txt", "r")
artistname = artist.readlines()[0]
print(artistname)
artist.close()
y = 0
songGuess = input("What is the song called?")
while(y<=2):
if songGuess == songname:
print("Answer correct!")
break
else:
y = y + 1
songguess = input("Incorrect! try again")
if y == 1:#
print("GAME OVER")
break
答案 0 :(得分:0)
这可能有效。我还对您的代码进行了一些编辑(这仍然很不完善)。
import random
for x in range(0, 1):
username= input("Please enter your username: ")
password= input("Please enter your password: ")
randNum = int(random.randint(0, 1))
with open("Songs.txt", "r") as song_f:
songname = str(song_f.readlines()[0])
print(songname[0])
with open("Artists.txt", "r") as artist_f:
artistname = artist_f.readlines()[0]
print(artistname)
songGuess = input("What is the song called?")
y = 0
score = 0
while(y<2):
if songGuess.lower() == songname.lower():
print("Answer correct!")
if (y==0):
score = 3
elif (y==1):
score = 1
break
else:
y = y + 1
songGuess = input("Incorrect! try again")
with open("usernamepasswordscore.txt","a") as f:
f.write("{} {} {}\n".format(username, password, score))