简体   繁体   中英

python quiz, picking a random query from a list

the problems i am facing with this program are, I'm trying to ask the user questions until the users score questionsright() is equal to 5 right answers. At the moment it just keeps asking questions regardless of score.

And I'm also trying to get the questions to be asked in a random order rather than just straight down the list every time, not sure how to do this. any help is appreciated, thanks

import random

def questionchal(questionText, answer):
    if input(questionText + ' ') == answer:
        return True

def gamelist():
    game = input('select a game, game 1, game 2 or game 3 ')
    if game == 'game 1':
        print('okay')
    if game == 'game 2':
        print('okayy')
    if game == 'game 3':
        print('okayyy')

questionsright = 0

#creates a list of questions and their answers
questionList = [
        ('What is my name?' , 'john'),
        ('Where do i live?' , 'uni'),
        ('Am i happy or sad?' , 'happy'),
        ('Am i hungry or full?' , 'hungry'),
        ('Am i alseep or awake?' , 'awake'),
        ('how old am i?' , '19')
    ]

for questionText, answer in questionList:
    while questionchal(questionText, answer) is not True:
        print('Wrong, heres another question!')
    print('Correct! Next question!')
    questionsright +=1
    print(questionsright)

if questionsright <5:
    for questionText, answer in questionList:
        while questionchal(questionText, answer) is not True:
            print('Wrong, heres another question!')
            print('Correct! Next question!')
            questionsright +=1
            print(questionsright)
else:
    print('Thanks for playing')
    gamelist()

Is something like this what you're looking for? It repeats the set of questions until the amount of right answers is >= 5. However, the way you've written it, it repeatedly asks the same question until you get it right. Is this what you intended?

import random

def questionchal(questionText, answer):
    if input(questionText + ' ') == answer:
        return True

def gamelist():
    game = input('select a game, game 1, game 2 or game 3 ')
    if game == 'game 1':
        print('okay')
    if game == 'game 2':
        print('okayy')
    if game == 'game 3':
        print('okayyy')

questionsright = 0

#creates a list of questions and their answers
questionList = [
        ('What is my name?' , 'john'),
        ('Where do i live?' , 'uni'),
        ('Am i happy or sad?' , 'happy'),
        ('Am i hungry or full?' , 'hungry'),
        ('Am i alseep or awake?' , 'awake'),
        ('how old am i?' , '19')
    ]

if __name__ == "__main__":
    while questionsright < 5:
        for questionText, answer in questionList:
            while questionchal(questionText, answer) is not True:
                print('Wrong, heres another question!')
            print('Correct! Next question!')
            questionsright +=1
            print(questionsright)

    else:
        print('Thanks for playing')
        gamelist()

and if you want it to skip to the next question if the user gets it wrong, you can do this:

if __name__ == "__main__":
    while questionsright < 5:
        for questionText, answer in questionList:
            if questionchal(questionText, answer):
                print('Correct! Next question!')
                questionsright +=1
                print(questionsright)
            else:
                print('Wrong, heres another question!')

    else:
        print('Thanks for playing')
        gamelist()

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