What's wrong with my Quiz Project made in Python?

Question:

I tried to make a Quiz Game using Python, inspired by a youtube video, I checked everything and it seems to be right, but it isn’t working.
First it can’t distinguish a right answer from a wrong answer, it always identifies that you picked the wrong answer, even if you hit the question.
Secondly, it does show the right answers, but it can’t show your guesses, and it’s followed by some other errors messages.

The code is right below:
(I made some meme questions in the quiz, hope you don’t mind haha)


def new_game():
    guesses = []
    correct_guesses: 0
    question_num = 1

    for key in questions:
        print("nn")
        print(key)

        for item in options[question_num-1]:
            print(item)

        guess = input("Enter A, B, C or D: ")
        
        guess = guess.upper
        
        guesses.append(guess)


        question_num += 1


        correct_guesses = check_answer(questions.get(key), guesses)


    display_score(correct_guesses, guesses)


def check_answer(answer, guess):
    
    if answer == guess:
        print("CORRECT! NICE")
        return 1 
    else:
        print("nnNooo, it's wrong")
    return 0
    




def display_score(guesses, correct_guesses):
    print("---------------------")
    print("RESULTS")
    print("---------------------")

    print("Answers: ", end="")

    for i in questions:
        print(questions.get(i), end=" ")
    print()



    print("Guesses: ", end="")

    for item in guesses:
        print(item, end=" ")
    print()

    
    score = int((correct_guesses/len(questions)) * 100)
    print("Your score is: " + str(score) + "%")
            
    





def play_again():
    response = input("Do you wanna play again?"  "(yes or no?): ")
    response = response.lower


    if response == "yes":
        return True
    
    else:
        return False





questions = {"Qual a data de nascimento de Second?": "B",
"Qual o ditador favorito de Packat?": "B",
"Qual a segunda lei da termodinâmica?": "C",
"Qual a melhor obra asiática já feita?": "D"}





options = [["A. 25/12/2004", "B. 31/03/2004", "C. 31/04/2003", "D. 21/06/2003"],
["A. Josef Stalin", "B. Nicolás Maduro", "C. Xin Jin Ping", "D. Kim Jon Un"],
["A. Ação e reação", "B. Princípio da Conservação de Energia", "C. Entropia", "D. Princípio da Incerteza"],
["A. Naruto", "B. HunterxHunter", "C. One Piece", "D. Solo Leveling"]]




new_game()



while play_again:
    new_game()


print("Byeeee")


the errors messages that appear are:

RESULTS
---------------------
Answers: B B C D
Guesses: Traceback (most recent call last):
  File "c:UsersSegundinhoDocumentsProgramaçãoPythonLearningquiz game.py", line 104, in <module>
    new_game()
  File "c:UsersSegundinhoDocumentsProgramaçãoPythonLearningquiz game.py", line 27, in new_game
    display_score(correct_guesses, guesses)
  File "c:UsersSegundinhoDocumentsProgramaçãoPythonLearningquiz game.py", line 58, in display_score
    for item in guesses:
TypeError: 'int' object is not iterable



Can someone please help me out?

Asked By: luiz_second

||

Answers:

Welcome to Stack-Overflow Luiz, here is a way to debug your code and understand the possible source of the error:

1) Look at the error message with attention

RESULTS
---------------------
Answers: B B C D
Guesses: Traceback (most recent call last):
  File "c:UsersSegundinhoDocumentsProgramaçãoPythonLearningquiz game.py", line 104, in <module>
    new_game()
  File "c:UsersSegundinhoDocumentsProgramaçãoPythonLearningquiz game.py", line 27, in new_game
    display_score(correct_guesses, guesses)
  File "c:UsersSegundinhoDocumentsProgramaçãoPythonLearningquiz game.py", line 58, in display_score
    for item in guesses:
TypeError: 'int' object is not iterable

Do you know what all the words in the line TypeError: 'int' object is not iterable mean? If not search each one that you do not understand on Google.

This error means that it is not possible to iterate (loop over) a number, because a number is only one thing, while you need a collection of things to loop over them.

2) Print the variables before the error

A cool trick to debug is using locals() to print all the variables with their names and values, doing it you get:

I add:

print(locals())

before the incriminated line:

for item in guesses:

Doing so I can see the output:

Guesses: {'correct_guesses': [<built-in method upper of str object at 0x7fe72522bf70>,
                     <built-in method upper of str object at 0x7fe72522bf70>,
                     <built-in method upper of str object at 0x7fe72522bf70>,
                     <built-in method upper of str object at 0x7fe72522bf70>],
 'guesses': 0,
 'i': 'Qual a melhor obra asiática já feita?',
 }

So now the cause of the error is clear, guesses is a number not a list, you probably meant to iterate over correct_guesses that is a list.

Answered By: Caridorc

there are a bunch of small errors sprinkled about the code. One of the bigger things is sometimes you look like you want to call a function like upper() but end up using the function itself as a value.

Here is something that is close to your current code with these small wrinkles taken out. Hopefully it works as you expect. There are lots of enhancements you can look to in the future like the use of enumerate() and zip() but this should get you back on track:

def new_game():
    guesses = []
    correct_guesses = 0
    question_num = 0

    for key in questions:
        print("nn")
        print(key)

        correct_answer = questions[key]
        possible_answers = options[question_num]

        for item in possible_answers:
            print(item)

        guess = input("Enter A, B, C or D: ")
        guess = guess.upper()
        guesses.append(guess)

        correct_guesses += check_answer(correct_answer, guess)

        question_num += 1

    display_score(guesses, correct_guesses)

def check_answer(answer, guess):
    if answer == guess:
        print("CORRECT! NICE")
        return 1 

    print("nnNooo, it's wrong")
    return 0

def display_score(guesses, correct_guesses):
    print("---------------------")
    print("RESULTS")
    print("---------------------")

    print("Corrent Answers: ", end="")
    for question in questions:
        print(questions.get(question), end=" ")
    print()

    print("Your Guesses: ", end="")
    for guess in guesses:
        print(guess, end=" ")
    print()

    score = int((correct_guesses/len(questions)) * 100)
    print("Your score is: " + str(score) + "%")

def play_again():
    response = input("Do you wanna play again?"  "(yes or no?): ")
    response = response.lower()
    return response == "yes"

questions = {
    "Qual a data de nascimento de Second?": "B",
    "Qual o ditador favorito de Packat?": "B",
    "Qual a segunda lei da termodinâmica?": "C",
    "Qual a melhor obra asiática já feita?": "D"
}

options = [
    ["A. 25/12/2004", "B. 31/03/2004", "C. 31/04/2003", "D. 21/06/2003"],
    ["A. Josef Stalin", "B. Nicolás Maduro", "C. Xin Jin Ping", "D. Kim Jon Un"],
    ["A. Ação e reação", "B. Princípio da Conservação de Energia", "C. Entropia", "D. Princípio da Incerteza"],
    ["A. Naruto", "B. HunterxHunter", "C. One Piece", "D. Solo Leveling"]
]

while True:
    new_game()
    if not play_again():
        break

print("Byeeee")
Answered By: JonSG
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.