Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Quiz and Readme files #444

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions scripts/Quizzing Game/Quizzing_Game.py
Original file line number Diff line number Diff line change
@@ -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")
16 changes: 16 additions & 0 deletions scripts/Quizzing Game/README.md
Original file line number Diff line number Diff line change
@@ -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.