lundi 21 octobre 2019

OR expression not working as intended in python [duplicate]

This question already has an answer here:

I'm trying to create a multiple choice quiz. The answer options are listed alongside the letters A, B, C and D.
I want the user to be able to give the answer itself or the letter associated with the correct answer.
My code works when I just use the code for the fully typed correct answer

if answerAttempt.lower() == correctAnswer.lower()

but when I use OR for the 'letter choice' any answer that is given is marked as correct.

if answerAttempt.lower() == correctAnswer.lower() or 'abcd'[answerOptions.index(correctAnswer)]

Any help would be appreciated. I can give more information if needed. If I can be clearer, please let me know as it is my first time posting. Here is the bulk of the code:

  if answerAttempt.lower() == 'quit':
    print('You scored ' + str(totalScore) + '/' + str(questionNum) + '.')
    sys.exit()

else:
    if answerAttempt.lower() == correctAnswer.lower() or 'abcd'[answerOptions.index(correctAnswer)]:
        print('Correct. The answer was %s. %s \n' % ('ABCD'[answerOptions.index(correctAnswer)], correctAnswer))
        totalScore += 1

    else:
        print('Incorrect. Your answer should have been %s. %s\n' % ('ABCD'[answerOptions.index(corr`enter code here`

Aucun commentaire:

Enregistrer un commentaire