In: Computer Science
Develop the following code for quiz (i am getting some errors)in python in such a manner that it shoulde be having extra 3 attempts(for wrong answrs) for all questions if the user entered wrong answer · for ex: If the user entered correct answer for first question #then 3 attempts will be carried to next questions. If the user entered 3 times wrong answer in 1st question itself means it sholud display as no more attempts and you got o out of 3
score=0
total_attempts=3
def question1(total_attempts,score):
print('What colors are apples?\na) Red/Green\nb) Orange\n')
answer1=input('enter answer: ')
if(answer1=='a'):
score=score+1
else:
print('try again')
attempts-=1
if attempts<=0:
print(' No More Attempts')
else:
question1(total_attempts,score)
return attempts,score
def question2 (total_attempts,score):
print('What colors are bananas?\na) Red/Green\nb) Yellow\n')
answer1=input('enter answer: ')
if(answer1=='b'):
score=score+1
else:
print('try again')
attempts-=1
if attempts<=0:
print(' No More Attempts')
else:
question2(attempts,score)
return attempts,score
def question3 (total_attempts,score):
print('What is the color of your hair?\na) Red/Green\nb)
Black\n')
answer1=input('enter answer: ')
if(answer1=='b'):
score=score+1
else:
print('try again')
attempts-=1
if attempts<=0:
print(' No More Attempts')
else:
question3(total_attempts,score)
return attempts,score
def questions():
kk=question1(total_attempts,score)
if attempts>0:
hh=question2(kk[0],kk[1])
if attempts>0:
q=question3(hh[0],hh[1])
print('score= ',q[1])
Code:
score=0
total_attempts=3
def question1(attempts,score):
print('What colors are apples?\na) Red/Green\nb) Orange\n')
answer1=input('enter answer: ')
if(answer1=='a'):
score=score+1
return attempts,score
else:
attempts-=1
if attempts<=0:
print('No More Attempts')
return attempts,score
else:
print('try again')
return question1(attempts,score)
def question2 (attempts,score):
print('What colors are bananas?\na) Red/Green\nb) Yellow\n')
answer1=input('enter answer: ')
if(answer1=='b'):
score=score+1
return attempts,score
else:
attempts-=1
if attempts<=0:
print('No More Attempts')
return attempts,score
else:
print('try again')
return question2(attempts,score)
def question3 (attempts,score):
print('What is the color of your hair?\na) Red/Green\nb) Black\n')
answer1=input('enter answer: ')
if(answer1=='b'):
score=score+1
return attempts,score
else:
attempts-=1
if attempts<=0:
print('No More Attempts')
return attempts,score
else:
print('try again')
return question3(attempts,score)
def questions():
kk=question1(total_attempts,score)
if kk[0]>0:
hh=question2(kk[0],kk[1])
if hh[0]>0:
q=question3(hh[0],hh[1])
print('score = ',q[1])
else:
print('score = ',hh[1])
else:
print('score = ',kk[1])
Please refer to the screenshot of the code to understand the indentation of the code:
Output (which can be done by calling questions() ):
1.
2.
3.
For any doubts or questions comment below.