Trying to do this: If my score (variable) reaches 10, the program ends and says “You win”. Whenever I do something like != 10, nothing happens

97
May 21, 2021, at 06:30 AM

I am trying to make it so when the score variable reaches 10, it says "You win!" or similar. Can anyone help? This is a quiz, where python chooses a line from a file, and when it reads the file, it knows the password to enter. The rest is basically you are guessing the names of songs, pulled from a file. I have included all the files, below. https://github.com/zobr0toN/Music-quiz-files

import random
score = 0
print("Welcome to my quiz. The task is to name 10 of Juice Wrld's songs. You get two wrong answers, and if you pass that margin you lose. Good luck!")
    
def login(): #defines the function
    file = open("musicpass.txt", "r")
    password = file.read()
    userguess = input("Enter password for access: ")
    if password == userguess:
        print("Access Granted!")
        game(song)
        ransong()
    else:
        print("Access Denied!")
        login()
def ransong():
    with open("musicsongs.txt", "r") as file:
        content= file.read()
        words = content.splitlines()
  
    # print random string
    #print(random.choice(words))
    return words

def game(song):
    songlist = song
    guess=0
    score=0
    while guess != 2:
        usguess1 = input("Enter your choice here: ")
        if usguess1 in songlist:
            print("Correct!")
            score = score + 1
        else:
            print("Incorrect, one more guess allowed")
            guess = guess + 1
    else:
        print("Incorrect, game over")
        print("Your score is:",score)
        print("Incorrect, no more guesses left.")
    while score != 10:
        print("Well done, you win!")    

def main():
    login()
song=ransong()
main()
Answer 1

This is a fairly common occurence while programming. Seeing as though you need to keep the program running even after a guess has been entered, but then have it exit once the score reaches 10, this is a good opportunity to use a while loop with the condition being that the guess != 2 and the score < 10. A while loop will keep repeating until certain conditions are met.

def game(song):
    songlist = song
    guess=0
    score=0
    while guess != 2 and score < 10:
        usguess1 = input("Enter your choice here: ")
        if usguess1 in songlist:
            score += 1
            print("Correct!")
Answer 2

You could add a break if the score reaches 10. Then the game stops.

while guess != 2:
    if score == 10:
       print("Well done, you win!")
       break
    usguess1 = input("Enter your choice here: ")
    if usguess1 in songlist:
        print("Correct!")
        score = score + 1
    else:
        print("Incorrect, one more guess allowed")
        guess = guess + 1
READ ALSO
How to load image if the image is available on AWS?

How to load image if the image is available on AWS?

I upload image from vue, to serverThe server will return the image url, but it's will process the image first, and then upload it to AWS

30
Google Maps null object reference on move camera method

Google Maps null object reference on move camera method

I am trying to work on google maps ie

77
Migration to AndroidX, using no longer import android.* packages? [duplicate]

Migration to AndroidX, using no longer import android.* packages? [duplicate]

According to documentation: https://developerandroid

56
Add data table pagination

Add data table pagination

I want to add data table pagination:

57