Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os # os.system('') dos command
- import random
- from datetime import datetime
- marks = 0
- count = 1
- name = input("Enter Username: ")
- with open("quiz.txt", "r") as qfile:
- questions = qfile.readlines()
- random.shuffle(questions)
- total_q = len(questions)
- # Get the current timestamp
- current_time = datetime.now()
- # Format the timestamp as a string
- formatted_timestamp = current_time.strftime("%Y_%m_%d_%H_%M")
- filename = name + formatted_timestamp + ".txt"
- with open(filename, "w") as out_file:
- for q1 in questions:
- os.system("cls")
- q1 = q1.strip()
- qlist = q1.split("#") # ['q','c','ca']
- # print(qlist)
- q = qlist[0]
- choices = qlist[1]
- correct_answer = qlist[2]
- print(
- f"Quiz [{count}/{total_q}] Marks[{marks}]"
- )
- out_file.write(
- f"Quiz [{count}/{total_q}] Marks[{marks}]\n"
- )
- print(q)
- print(choices)
- answer = input("Enter your choice: a,b,c but x to exit: ")
- out_file.write(f"Question: {q}\n")
- out_file.write(f"Choices: {choices}\n")
- out_file.write(f"Answer: {answer}\n")
- if answer == "x":
- print("Bye!")
- exit(0)
- elif answer == correct_answer:
- print("Correct")
- out_file.write(f"Correct Answer\n")
- marks += 1
- else:
- print("Incorrect")
- out_file.write(f"Incorrect Answer\n")
- count += 1
- out_file.write(f"Total Marks Become {marks}\n\n")
- os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement