0

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()
Ceal Clem
  • 225
  • 4
  • 10
  • As you note, your code is long. You should reduce it to a [mcve]. You may want to read [ask]. Then describe your attempt to solve the *specific* problem and what deviates from your expectations. – Arya McCarthy May 30 '17 at 13:26
  • thanks for the feedback! i'll try and be more minimal next time – Ceal Clem May 30 '17 at 18:26

2 Answers2

0

You can use another list(for example correct_answer or such) to record the correct answer for each question. Then instead of using a1 = answers.pop(random_item) to choose a wrong answer, use

while True:
    if answer[random.randint(0, len(answers)-1)] != correct_answer[question]:
        break
a1 = answers.pop(id)

to avoid choosing the correct answer as an incorrect one.

EDIT

As your correct answer is already in the answers, you should not pop items when choosing some incorrect answers, otherwise it will break the indices.

You can choose two wrong answers and a correct answer like this.

correct = question_index

while True:
    wrong1 = random.randint(0, len(answers)-1)
    if wrong1 != correct:
        break

while True:
    wrong2 = random.randint(0, len(answers)-1)
    if wrong1 != wrong2 and wrong1 != correct:
        break
TsReaper
  • 845
  • 7
  • 19
0

You could structure the data in a simpler way, using dictionaries. Each item is a pair of a key and a value, for more info refer to this. For example your data will look like this (each question is associated with an answer):

data = {'question1': 'answer1', 'question2': 'answer2'}

Then we can loop through this dictionary to print the answers after we randomly choose a question. Here's some sudo code for help:

# Choose a random question

# Print all the answers in the dictionary
for key, value in data.iteritems(): #its data.items() in Python 3.x
     print value

# If the choice matches the answer
pop the question from the dictionary and do whatever you want to do

# Else, ask again

For more information about iterating through a dictionary refer to this.

mrhallak
  • 1,138
  • 1
  • 12
  • 27