简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM