Added Quiz and Readme files

This commit is contained in:
Ramakm 2022-10-15 18:00:43 +05:30
parent 4eda20bccd
commit 5adeffedaa
2 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,47 @@
import requests
import json
import pprint
import random
import html
url="https://opentdb.com/api.php?amount=1"
score_correct=0
score_incorrect=0
endGame=""
while endGame!='quit':
r=requests.get(url)
if(r.status_code!=200):
endGame=input("Sorry there was a problem retrieving the question. Please press enter to try again or type 'quit' to quit the game.")
else:
answer_number=1
data=json.loads(r.text)
question=data['results'][0]['question']
answers=data['results'][0]['incorrect_answers']
correct_answer=data['results'][0]['correct_answer']
answers.append(correct_answer)
random.shuffle(answers)
valid_answer=False
print(html.unescape(question)+"\n")
for answer in answers:
print(str(answer_number)+"- "+ html.unescape(answer))
answer_number+=1
while valid_answer==False:
user_answer=input("\n Type the number of the correct answer ")
try:
user_answer=int(user_answer)
if(user_answer>len(answers) or user_answer<=0):
print("Invalid Answer")
else:
valid_answer=True
except:
print("Invalid answer. Use only numbers")
user_answer=answers[int(user_answer)-1]
if(user_answer==correct_answer):
print("Congratulations you answered correctly.")
score_correct+=1
else:
print("Sorry, " + html.unescape(user_answer) + " is the incorrect answer. The correct answer is "+ html.unescape(correct_answer)+".")
score_incorrect+=1
print("Correct: "+str(score_correct)+"\nIncorrect: "+str(score_incorrect))
endGame=input("Press enter to play again or type 'quit' to quit the game ")
print("\nThanks for playing")

View File

@ -0,0 +1,16 @@
# Module Installalations:
pip install requests
pip install jsonlib (Most of time this will be there)
## How this code works:
I have used request module here to access the url https://opentdb.com/api.php?amount=1
Once you run the code, One question will come, You just need to give the option number 1,2,3 or 4.
It will continue until you won't type 'quit' in your terminal.
The output will keep counting the no of correct and incorrect answers given by you till you quit.