Search code examples
pythonnumberspoints

how do you add points on your python code


i need help with a code that can calculate the points for every guess the user try. if the user get the answer right from the first try it will be 3 points, if its from the second guess then its 1 point. when the user win or lose it stores his username and score in a external file. when all users had a go then it displays the top 5 winners from the external file.

i have already done the part that asks the user for the username and password and stores it in an external device. i have also wrote down a code to display the artists name and the first letter of the song and give the user 2 tries.

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

Solution

  • This could work. I also edited your code a bit (it's still far from perfect).

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