Advertisement
slashdemonofthesword

Python Quiz Program

May 14th, 2017
1,152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.92 KB | None | 0 0
  1. import os
  2. import linecache
  3.  
  4.  
  5. # IMPORTANT:
  6. # IMPORTANT: TO USE THIS, MAKE A FOLDER CALLED 'QUESTION SETS' AND PLACE IT IN THE SAME SPOT AS THIS FILE.
  7. # IMPORTANT
  8.  
  9. exitMainGameLoop = False # Be the main loop for the program. If this is equal to true, the program ends
  10. cwd = os.getcwd() # Get's the programs working directory
  11. print()
  12. print("Welcome to this python quiz. Would you like to play or make your own quiz? (Please Select a number)")
  13. print("1. Play ")
  14. print("2. Make")
  15. print("3. About")
  16. gameMode = input()
  17.  
  18. userInput = "n"
  19.  
  20. if gameMode == "1":
  21.  
  22.     while exitMainGameLoop == False:
  23.  
  24.         userInput = ""
  25.         print()
  26.         print("Now in quiz play mode")
  27.         print()
  28.  
  29.  
  30.         currentQuestionSet = []
  31.  
  32.         fileIndex = 1
  33.  
  34.         files = []
  35.         for entry in os.scandir(cwd + "\\Question Sets"): # Checks for the files inside of the Questions Sets folder which is located in the source folder of this program
  36.             if entry.is_file:
  37.                 files.append(str(fileIndex) + ". " + entry.name) # Add the names of the files that has been found to the files[] list so it can be printed later in a neat fashion and displayed to the user
  38.                 fileIndex = fileIndex + 1
  39.  
  40.         for doc in files: # Print out all of the files that has been found
  41.             print(doc)
  42.  
  43.         print()
  44.         print("Here are your available quizes")
  45.         fileName = input("Please choose the number next to your quiz to select it: ")
  46.         print()
  47.  
  48.  
  49.  
  50.  
  51.         fileName = files[int(fileName) - 1]
  52.         fileName = fileName[3:] # Slices of '#. ' i.e '1. Random.txt' becomes 'Random.txt' This is done becomes the files do not have numbers attached to them like it is desplayed to the user.
  53.  
  54.         i = 1 # For the while loop. Can be zero, but chose one so I don't have to do math more than I need to
  55.         line = 2 # Used for getting the proper lines to display to the user. These lines will be the question followed by it's respective answers.
  56.                 #sThe line number and the answer number will not be displayed. And only one questions/answer will show at a time
  57.  
  58.  
  59.         rightAnswerLine = 7 # This is the line number for the right answer (The first one)
  60.  
  61.         while i <= int(linecache.getline(cwd + "/Question Sets/" + fileName, 1)): # Get's the first line of the file and converts it to an int
  62.             print()
  63.             print(linecache.getline(cwd + "/Question Sets/" + fileName, line)) # This line will show the question being asked
  64.             line = line + 1
  65.             print(linecache.getline(cwd + "/Question Sets/" + fileName, line)) # This line will display answer choice one
  66.             line = line + 1
  67.             print(linecache.getline(cwd + "/Question Sets/" + fileName, line)) # This line will display answer choice Two
  68.             line = line + 1
  69.             print(linecache.getline(cwd + "/Question Sets/" + fileName, line)) # This line will display answer choice Three
  70.             line = line + 1
  71.             print(linecache.getline(cwd + "/Question Sets/" + fileName, line)) # This line will display answer choice Four
  72.  
  73.             rightAnswer = linecache.getline(cwd + "/Question Sets/" + fileName, rightAnswerLine) # Sets the rightAnswer to the proper line. Python doesn't read the line if this isn't done this way
  74.  
  75.             userInput = ""
  76.             userInput = input("Which is the right answer? (Please select the number you want)")
  77.  
  78.  
  79.  
  80.             # These were type casted to int as leaving them as strings would cause an error. Even if the right answre was chosen, it woud still be wrong
  81.             # Having them as int fixes that problem.
  82.             userInput = int(userInput)
  83.             rightAnswer = int(rightAnswer)
  84.  
  85.             print("The right answer is {}".format(rightAnswer))
  86.  
  87.             if userInput == rightAnswer:
  88.                 print("{} is the right answer!".format(userInput))
  89.                 print("Moving on to the next question")
  90.                 line = line + 2
  91.                 rightAnswerLine = rightAnswerLine + 6
  92.                 i = i + 1
  93.             else:
  94.                 print("{} is not the right answer.".format(userInput))
  95.                 print("Moving on to the next question")
  96.                 line = line + 2
  97.                 rightAnswerLine = rightAnswerLine + 6
  98.                 i = i + 1
  99.  
  100.         userInput = ""
  101.         exitMainGameLoop = True
  102.  
  103. elif gameMode == "2":
  104.  
  105.     while exitMainGameLoop == False:
  106.  
  107.         question = " " # This will store the question
  108.         i = 1 # Used for iterating through while loop that creates the question sets
  109.         j = 0 # Used for iterating thorugh the while loop that stores and saves the answers
  110.         answerList = [0, 0, 0, 0] # Going to store the answers to the questions
  111.  
  112.  
  113.         filesInDir = []
  114.         for entry in os.scandir(cwd + "/Question Sets"):
  115.             if entry.is_file:
  116.                 filesInDir.append(entry.name)
  117.  
  118.         for f in filesInDir:
  119.             print(f)
  120.  
  121.  
  122.         fileName = input("Please enter the name of this Question Set: ")
  123.         fileName = fileName + ".txt" # This will make sure the file created is a text file by adding .txt to fileName
  124.         path = os.path.exists(cwd + "/Question Sets/" + fileName) # This will be used to see if the file has already been created when writing the questions to the file.
  125.  
  126.  
  127.  
  128.  
  129.         if path == True: # checks if the user wants to rewrite an existing file
  130.             userInput = input("'{}' already exists. Do you wish to rewrite it? ( y / n)".format(fileName))
  131.  
  132.             # check what the user types then runs the proper message. This only runs if path == True
  133.             if userInput == "y":
  134.                 print("The file has been overwritten")
  135.                 numberOfQuestions = input("How many questions do you want? ") # Asks for the number of desired questions
  136.                 numberOfQuestions = int(numberOfQuestions) # Converts the string into and int (integer)
  137.                 with open("Question Sets/" + fileName, 'w') as f: # Writes the number of questiosn to the file before the loop begins
  138.                     f.write(str(numberOfQuestions) + "\n")
  139.  
  140.             elif userInput == "n":
  141.                 print("The file will not be overwritten")
  142.             else:
  143.                 print("You did not either 'y'  or 'n'. The program will now close.")
  144.                 quit()
  145.  
  146.         else:
  147.             with open("Question Sets/" + fileName, 'w') as f: # Writes the number of questiosn to the file before the loop begins
  148.                 numberOfQuestions = input("How many questions do you want? ") # Asks for the number of desired questions
  149.                 numberOfQuestions = int(numberOfQuestions) # Converts the string into and int (integer)
  150.                 f.write(str(numberOfQuestions) + "\n")
  151.  
  152.  
  153.  
  154.         while i <= numberOfQuestions:
  155.             userQuestion = input("Please enter your question: ") # Ask user for quesetion
  156.  
  157.  
  158.  
  159.             while j < len(answerList):
  160.                 answerList[j] = input("What is your {} answer (Please make sure one of these contains the write answer)".format(j + 1)) # Promts the user to enter a new answer. Is stored in the answerList at index 'i'
  161.                 answerList[j] = str(j + 1) + ". " + answerList[j]
  162.                 j = j + 1 # Increases i by one each after you enter an aswer
  163.  
  164.             for answer in answerList:
  165.                 print(answer)
  166.  
  167.             rightAnswer = 0
  168.             rightAnswer = input("Which one of the answers above is the right answr? (please choose the number next to the question i.e '1' for {})".format(answerList[0])) # Ask the user to select the right answer
  169.  
  170.  
  171.  
  172.             j = 0
  173.             if path == True: # If the file exists, and the player wants to rewrite it, remake it so it's empty then write the needed contents
  174.                 if userInput == "y":
  175.                     with open(cwd + "/Question Sets/" + fileName, 'a+') as f:
  176.                         f.write(userQuestion + "\n") # Saves question
  177.                         for answer in answerList: # saves the answers in answerList to one line
  178.                             f.write(answerList[j] + "\n")
  179.                             j = j + 1
  180.                             userInput = "n" # change the user input to 'n' otherwise the file will be overwritten again for the second question
  181.                         f.write(rightAnswer + "\n") # Saves the number to the right answer.
  182.                 elif userInput == "n":
  183.                     with open("Question Sets/" + fileName, 'a+') as f: # Creates a new file called 'test.txt' and uses append mode. Called f for naming convention
  184.                         f.write(userQuestion + "\n") # Saves question
  185.                         for answer in answerList: # saves the answers in answerList to one line
  186.                             f.write(answerList[j] + "\n")
  187.                             j = j + 1
  188.                         f.write(rightAnswer + "\n") # Saves the number to the right answer
  189.  
  190.             else: # if the file doesn't exist, create it with the contents needed
  191.                 with open("Question Sets/" + fileName, 'a+') as f: # Creates a new file called 'test.txt' and uses append mode. Called f for naming convention
  192.                     f.write(userQuestion + "\n") # Saves question
  193.                     for answer in answerList: # saves the answers in answerList to one line
  194.                         f.write(answerList[j] + "\n")
  195.                         j = j + 1
  196.                     f.write(rightAnswer + "\n") # Saves the number to the right answer
  197.  
  198.             j = 0
  199.  
  200.             i = i + 1
  201.  
  202.         i = 0
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.         exitMainGameLoop = True # END OF MAIN GAME LOOP
  210.  
  211. elif gameMode == "3":
  212.     print("Hello Everyone, I'm Anguel Esperanza, the developer of this small python script.")
  213.     print("I made this in the hopes that it can help me study while I'm at college.")
  214.     print("I figured I'd release this script and post it online in case anyone else wants to look at it, use it, or modify it.")
  215.     print("This isn't a feature packed script. All you can do is make questions sets, and play those questions sets.")
  216.     print("I would love to add more features in the future. I have plans to do so, however I'm going to need to learn more python before than.")
  217.     print("Making this was a lot of fun. I did my best to document the code so it's easy to understand.")
  218.     print("I hope you all enjoy this program and find it useful in your studies.")
  219.  
  220.     print("----------------COPY RIGHT INFORMATION-----------------")
  221.     print("You are free to use this code in any project you want, both commerical and non-commercial use.")
  222.     print("I only ask that you give me credit and link to either one or both of the following links")
  223.     print("Github: https://github.com/fictionalreality/PythonQuizProgram")
  224.     print("Pastebin: https://pastebin.com/zNW2VG1K")
  225.     print("These are the places that this soucre code has been published too. If this changes in the future, these links will be updated as such.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement