1

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
martineau
  • 119,623
  • 25
  • 170
  • 301
Doris Ng
  • 13
  • 2
  • `for answer in random.choice(quizzes):` select a *question* string, then iterates over *each character of the string one-by-one*. `random.choice(quizzes)` on a line by itself does nothing. `quizzes` always equals a list of three strings, so will never compare to a single string like `q1`. Lots of bugs. Learn to use a source debugger and step through the code looking at the values of your data. – Mark Tolonen Sep 29 '20 at 22:30

2 Answers2

0

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.")
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • Thank you for introduce "while". Even I tested this code still keep looping but it fix most of my problem(can't get the right answer) and I definitely use while to stop my loop by >while guesses >0: instead of while True. – Doris Ng Oct 01 '20 at 21:17
  • The `break` statements should be exiting the `while True` loop. The only way the `for quiz in quizzes` loop will exit is if you've gotten the right answer for all 3, or gotten 3 wrong guesses for each one, or some combination of the two. If you want to leave it early then you'll have to `break` outside of the `while True` loop. – Random Davis Oct 01 '20 at 21:19
-1

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
sykezlol
  • 88
  • 9