python测验,从列表中选择一个随机查询

时间:2014-11-18 17:07:47

标签: python random

我在这个程序中面临的问题是,我试图询问用户问题,直到用户得分问题()等于5个正确的答案。目前,无论得分如何,它都会不断提问。

我也试图以随机顺序提出问题,而不是每次都直接在列表中询问,不知道如何做到这一点。感谢任何帮助,谢谢

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

1 个答案:

答案 0 :(得分:0)

这是你想要的东西吗?它会重复这组问题,直到正确答案的数量大于等于5.但是,按照你编写它的方式,它会反复询问同样的问题,直到你做对了。这是你的意图吗?

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

如果你想让它跳到下一个问题,如果用户弄错了,你可以这样做:

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()
相关问题