mardi 30 mai 2017

Multiple choice quiz in python using random function and lists

I'm trying to make a multiple choice quiz using python. It seemed like something simple to make at first but now i'm scratching my head a lot trying to figure out how to do certain things.

I am using two lists; one for the questions and one for the answers. I would like to make it so that a random question is chosen from the questions list, as well as 2 random items from the answers list (wrong answers) and finally the correct answer (which has the same index as the randomly selected question). I've got as far as selecting a random question and two random wrong answers

  • My first problem is getting the program to display the correct answer.
  • The second problem is how to check whether the user enters the correct answer or not.

I would appreciate any feedback for my code. I'm very new to this so go a little easy please! I hope my stuff is readable (sorry it's a bit long)

thanks in advance

import random

print ("Welcome to your giongo quiz\n")
x=0
while True:
    def begin(): # would you like to begin? yes/no... leaves if no
        wanna_begin = input("Would you like to begin?: ").lower()
        if wanna_begin == ("yes"):
            print ("Ok let's go!\n")
            get_username()#gets the user name
        elif wanna_begin != ("no") and wanna_begin != ("yes"):
            print ("I don't understand, please enter yes or no:")
            return begin()
        elif wanna_begin == ("no"):
            print ("Ok seeya!")
    break

def get_username(): #get's username, checks whether it's the right length, says hello 
    username = input("Please choose a username (1-10 caracters):")
    if len(username)>10 or len(username)<1:
        print("Username too long or too short, try again")
        return get_username()
    else:
        print ("Hello", username, ", let's get started\n\n") 
        return randomq(questions)

##########

questions = ["waku waku", "goro goro", "kushu", "bukubuku", "wai-wai", "poro", "kipashi", "juru", "pero"]

answers = ["excited", "cat purring", "sneezing", "bubbling", "children playing", "teardrops falling", "crunch", "slurp", "licking"]

score = 0

if len(questions) > 0: #I put this here because if i left it in ONly inside the function, it didnt work...
        random_item = random.randint(0, len(questions)-1)
        asked_question = questions.pop(random_item)

###########

def randomq(questions):
    if len(questions) > 0:
        random_item = random.randint(0, len(questions)-1)
        asked_question = questions.pop(random_item)
        print ("what does this onomatopea correspond to?\n\n%s" % asked_question, ":\n" )
        return choices(answers, random_item)
    else:
        print("You have answered all the questions")
        #return final_score

def choices(answers, random_item):
    random_item = random.randint(0, len(questions)-1)
    a1 = answers.pop(random_item)
    a2 = answers.pop(random_item)
    possible_ans = [a1, a2, asked_question"""must find way for this to be right answer"""]
    random.shuffle(possible_ans)
    n = 1
    for i in possible_ans:
        print (n, "-", i) 
        n +=1
    choice = input("Enter your choice :")
    """return check_answer(asked_question, choice)"""

    a = questions.index(asked_question)
    b = answers.index(choice)
    if a == b:
        return True
    return False

begin()




Aucun commentaire:

Enregistrer un commentaire