Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Alpmms authored Feb 22, 2024
1 parent 5089428 commit 8431840
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Python quiz application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python
# coding: utf-8

# In[ ]:


# quiz.py

questions = [
{
"question": "What is the output of print(2 * 3 ** 3)",
"answers": ["18", "54", "6", "27"],
"correct_answer": "54"
},
{
"question": "Which of the following is not a Python data type?",
"answers": ["str", "int", "float", "tuple"],
"correct_answer": "tuple"
},
{
"question": "What is the output of print(abs(-5))",
"answers": ["5", "-5", "0", "None"],
"correct_answer": "5"
}
]

def ask_question(question):
print(question["question"])
for i, answer in enumerate(question["answers"], 1):
print(f"{i}. {answer}")
user_answer = input("Enter your answer (1-{}): ".format(len(question["answers"])))
return user_answer

def check_answer(question, user_answer):
if user_answer == "":
return False
if user_answer == question["correct_answer"]:
print("Correct!")
return True
else:
print("Incorrect. The correct answer is '{}'.".format(question["correct_answer"]))
return False

def run_quiz():
score = 0
for question in questions:
user_answer = ask_question(question)
if check_answer(question, user_answer):
score += 1
print("Your final score is: {}/{}".format(score, len(questions)))

if __name__ == "__main__":
run_quiz()


# In[ ]:




0 comments on commit 8431840

Please sign in to comment.