How can I fix this code with unstoppable loop?

111 views Asked by At

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
2

There are 2 answers

2
Random Davis On BEST ANSWER

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.")
0
sykezlol On

Your problem is the placement of guesses. Right now it is inside your for loop. Put it outside your for loop and it should work.

You should also take a look at your

elif guesses == 0:

Right now the player will be able to guess even with 0 guesses. It should probably be <=1 instead.

Your code will behave the way you want like this:

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
guesses = 3    

for answer in random.choice(quizzes):
           # 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 <=1:
                  print( 'Sorry, you are reached the maximum guesses. Bye~now~')
                  exit()

                  
           else:
                  guesses -= 1        #reducing the max. guesses of 3 to 2 to 1
                  print( "Sorry, it's incorrect.")
            
           result = asking