如何在 python 代码上加分

how do you add points on your python code

我需要有关代码的帮助,该代码可以为用户尝试的每一次猜测计算分数。如果用户从第一次尝试中得到正确的答案,它将得到 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

这可行。我还对您的代码进行了一些编辑(它还远非完美)。

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))