My code is a quiz - along the lines of which character in 'The book' are you most like - basically a series of questions with options of a,b,c,d,e as the answers.
`#Question 1
print("This is the text of question 1")
print("a - This is the text of answer a")
print("b - This is the text of answer b")
print("c - This is the text of answer c")
print("d - This is the text of answer d")
print("e - This is the text of answer e")
quest_ans()
answers.append(quest_ans.ans)`
The code next calls a function to allow the user input their answer, the function also takes that answer and converts it to a number, which is passed and appened to a list, for summing at the end.
This is the function quest_ans()
def quest_ans():
ans=0
answer=input(str.lower("Please Enter a,b,c,d or e: "))
if answer.lower() == 'a':
quest_ans.ans =2
elif answer.lower() == 'b':
quest_ans.ans=3
elif answer.lower() == 'c':
quest_ans.ans=4
elif answer.lower() == 'd':
quest_ans.ans=5
elif answer.lower() == 'e':
quest_ans.ans=6
return quest_ans.ans
As long as the user enters the one of the correct answer options - this all works fine, but I want to manage if the user enters something other than a, b, c, d, e by printing a message to alert them and re calling the function to allow them enter a correct answer.
I've tried try except blocks and while loops and I can't get the syntax right.
Any help would be appreciated.
Abbie