Advertisement
DrAungWinHtut

quiz_exam.py

Jan 16th, 2025
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import os  # os.system('') dos command
  2. import random
  3. from datetime import datetime
  4.  
  5. marks = 0
  6. count = 1
  7.  
  8. name = input("Enter Username: ")
  9.  
  10. with open("quiz.txt", "r") as qfile:
  11.     questions = qfile.readlines()
  12.  
  13. random.shuffle(questions)
  14. total_q = len(questions)
  15. # Get the current timestamp
  16. current_time = datetime.now()
  17. # Format the timestamp as a string
  18. formatted_timestamp = current_time.strftime("%Y_%m_%d_%H_%M")
  19. filename = name + formatted_timestamp + ".txt"
  20. with open(filename, "w") as out_file:
  21.     for q1 in questions:
  22.         os.system("cls")
  23.         q1 = q1.strip()
  24.         qlist = q1.split("#")  # ['q','c','ca']
  25.         # print(qlist)
  26.         q = qlist[0]
  27.         choices = qlist[1]
  28.         correct_answer = qlist[2]
  29.  
  30.         print(
  31.             f"Quiz [{count}/{total_q}]                                                  Marks[{marks}]"
  32.         )
  33.         out_file.write(
  34.             f"Quiz [{count}/{total_q}]                                                  Marks[{marks}]\n"
  35.         )
  36.         print(q)
  37.         print(choices)
  38.         answer = input("Enter your choice: a,b,c but x to exit: ")
  39.  
  40.         out_file.write(f"Question: {q}\n")
  41.         out_file.write(f"Choices: {choices}\n")
  42.         out_file.write(f"Answer: {answer}\n")
  43.         if answer == "x":
  44.             print("Bye!")
  45.             exit(0)
  46.         elif answer == correct_answer:
  47.             print("Correct")
  48.             out_file.write(f"Correct Answer\n")
  49.             marks += 1
  50.         else:
  51.             print("Incorrect")
  52.             out_file.write(f"Incorrect Answer\n")
  53.         count += 1
  54.         out_file.write(f"Total Marks Become {marks}\n\n")
  55.         os.system("pause")
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement