Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Quiz program

Hello, I am writing a quiz program and I am having trouble with two things. The first is that I want to give the user 3 chances to answer the question before giving the answer and moving on to the next question. Second I want to keep score of how many the user got correctly. If anyone can point me in the right the direction that would be greatly appreciated. Here is my code

question_answer_dict = {"What is the kanji for 'Go'?": '行く', "What is the kanji for 'snow'? ": '雪'}


chances = 3
while True:
    name = input("Hello, to exit enter 'q' or Please enter your name: ")    
    if name == 'q':
        break
    try:
        for question,answer in question_answer_dict.items():
            print(question)
            user_response = input("Answer: ")
            if user_response != answer:
                    print("Sorry, {} that is incorrect, the answer is '{}'\n".format(name,answer))
                    chances -= 1
            else:
                user_response == answer
                print("Good job! {}, that is correct".format(name))
    except ValueError:
        continue

print("Thanks for taking the quiz")

2 Answers

try this: first change chances to 0. then in the try block add a if statement that checks if count is greater than 3, after each try u should 1 to chances because u want to keep track of how many tries he did, so u add chances++ and for the end u add the keyword continue which go back to the begging of the while loop. i’m not an expert in python but it should work.

Hello, First, thank you for taking the time to help me out. I did something similar to what you said but now I have a problem with my loop. I want it to keep asking the first question until I have 0 chances left. What it is doing now is asking the first question and the second question at the same time. Also if I input the correct answer it says "Sorry you are out of tries". Any help would be greatly appreciated.

question_answer_dict = {"What is the kanji for 'Go'?": '行く', "What is the kanji for 'snow'? ": '雪', "What is the Kanji for 'Now'?": '今'}



while True:
    name = input("Hello, to exit enter 'q' or Please enter your name: ")    
    if name == 'q':
        break
    try:
        tries = 3
        while tries != 0:
            for question,answer in question_answer_dict.items():
                print(question)
                user_response = input("Answer: ")
                if user_response != answer:
                    tries -= 1
                    print("Sorry, {} that is incorrect. Try again! You have {} tries left".format(name,tries))
                    print(question)
                else:
                    tries == 0
                    print ("Sorry you are out of tries")
                    break
        if user_response == answer:
                print("Good job! {}, that is correct".format(name))
    except ValueError:
        continue

print("Thanks for taking the quiz")

instead of while true, say while chances>3, declare chances before the while loop. i think u over complicate it, try to get 1 input at the same time and not the answer and the question. i don’t remember dictionary, that’s why i suggest that.

Thanks!Ill try it out. Thank-you for your help.