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
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; therandom.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, becausequizzes
is your list of quizzes, so checkingquizzes == q1
will always beFalse
. In my code, since I'm looping throughquizzes
viafor quiz in quizzes
, that means thatquiz
in my code is a quiz string fromquizzes
, so when I doquiz == q1
, that correctly compares the currentquiz
toq1
,q2
orq3
.I noticed you had your answers defined in
a1
,a2
anda3
, so you can simplify your code and use those for comparison toasking
, likeasking == a1
instead ofasking == 'A cold'
.