How can I fix this code with unstoppable loop?

Doris Ng

I am going to make a funny quiz random the 3 questions and giving 3 chances to guess.

After I tried to execute, I tried the correct answer but keep looping and tell my answer is incorrect and can't stop looping and I have no idea why....

Can anyone please help me to take a look?

q1 = 'What can one catch that is not thrown?'
a1 = 'A cold'
q2 = 'If you had only one match and entered a dark room containing an oil lamp, some kindling wood, and a newspaper, which would you light first?'
a2= 'The match'
q3 = 'Some months have 31 days, others have 30 days, but how many have 28 days?'
a3= 'All the months'

import random
quizzes = [q1, q2, q3]  #catagorise to quizzes

for answer in random.choice(quizzes):
    guesses = 3  # limits to 3 guesses
    random.choice(quizzes)
    asking = input('Your Answer is?\n')
    if   quizzes == q1 and asking == 'A cold':
        print( 'Bingo!!')
        break
    elif quizzes == q2 and asking == 'The match':
        print( 'Bingo!!')
        break
    elif quizzes == q3 and asking == 'All the months':
        print( 'Bingo!!')
        break
    elif guesses == 0:
        print( 'Sorry, you are reached the maximum guesses. Bye~now~')
    else:
        guesses -= 1  #reducing the max. guesses of 3 to 2 to 1
        print( "Sorry, it's incorrect.")
    result = asking
Random Davis

The line for answer in random.choice(quizzes) is actually getting a random quiz string, then iterating through each character in the string; that's why it seems like the loop isn't stopping, because it's iterating on more items than you'd expect.

The line you had originally in the loop, random.choice(quizzes), does nothing; the random.choice() function returns a random item from a list. If you're not doing anything with the returned value, like printing it, then nothing will happen.

Your original lines like if quizzes == q1 and asking == 'A cold': won't work, because quizzes is your list of quizzes, so checking quizzes == q1 will always be False. In my code, since I'm looping through quizzes via for quiz in quizzes, that means that quiz in my code is a quiz string from quizzes, so when I do quiz == q1, that correctly compares the current quiz to q1, q2 or q3.

I noticed you had your answers defined in a1, a2 and a3, so you can simplify your code and use those for comparison to asking, like asking == a1 instead of asking == 'A cold'.

import random
quizzes = [q1, q2, q3]  #catagorise to quizzes
random.shuffle(quizzes)  # randomize the quiz order

for quiz in quizzes:  # loop through the list of quizzes
    guesses = 3     # limits to 3 guesses
    print(quiz)     # prompt the user with the quiz question
    while True:     # keep asking for answers until guesses run out, or the user gets a correct answer
           asking = input('Your Answer is?\n')
           if     quiz == q1 and asking == a1:
                  print( 'Bingo!!')
                  break
           elif   quiz == q2 and asking == a2:
                  print( 'Bingo!!')
                  break
           elif   quiz == q3 and asking == a3:
                  print( 'Bingo!!')
                  break
           elif   guesses == 0:
                  print( 'Sorry, you are reached the maximum guesses. Bye~now~')
                  break
           else:
                  guesses -= 1        #reducing the max. guesses of 3 to 2 to 1
                  print( "Sorry, it's incorrect.")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I put this DRY code into a for loop?

How can I fix this while loop in my first python calculator?

How can I remove unstoppable link underlines under twentysixteen child themed Wordpress

How can I fix the slowness of my pygame event loop?

How do i fix the infinite loop that this code gets itself into?

how can I fix this loop for my dice rolling problem?

How can I fix this infinite loop caused by my function in Python?

How can I fix my code for an Arduino LED string controller?

How can i fix my code to loop right my Hangman Game?

How can I fix this code for WPF Application?

How should I fix my for loop code for summing a range?

How can i fix this?

How can I fix this game serialization infinite loop in libGdx?

JAVA,how can I loop my code

How can I fix this code, I keep receiving an infinite loop?

Unstoppable loop

How can I fix this simple do/while loop?

How can I fix my code to print "AAADADDA" instead of "DAADAAAAAAADADDA"?

how can i fix this error in a bison code?

How can I fix this code and add input inside the Elif statement?

Browser can't process the code, how can I fix it?

How can I fix this wrong result in matrix for loop in R?

How can i fix this

How can I create a for loop for this code?

How can I fix the issue with this for loop?

how can i fix this runtime error for a simple code written in c?

How can I fix this for loop to print results inside an Array with Objects?

there is an "endless loop" how ı can fix it

How can I insert a continuous loop into this code?