Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.08 KB | None | 0 0
  1. import tkinter as tk #module for window
  2. from tkinter import messagebox #module for message boxes
  3. import base64 #module for decoding answers
  4. import sys
  5. score = 0
  6. questions = ["When was the Mechanical pencil patented?",
  7.              "What is the largest pencil manufacturer?",
  8.              "Around how many pencils does Faber-Castell make per year in billions?",
  9.              "Does Ferrari make pencils?",
  10.              "What year were pencils invented?",
  11.              "How many pencils can a single tree make?",
  12.              "Which of these are pencil maunfacturers?"]
  13. answerse = [b'MTgyMg==',b'RmFiZXItQ2FzdGVsbA==',b'RmlsbGVy',b'Tm8=',b'MTc5NQ==',b'Mw==',b'RmlsbGVy'] #encoded answers
  14. q6options = [b"MywwMDANCg==",b"MzAsMDAwDQo=",b"MzAwLDAwMA0K",b"MywwMDAsMDAwDQo="]
  15. answers = [] #list for decoded answers
  16. attempt = [] #list for attempt
  17. xd = []
  18. praise = ["Awful","Bad", "OK", "Nice", "Well Done","Amazing","Perfect"] #In score show-er box
  19. for _ in answerse: #for every item in the encoded answers list
  20.     answers.append(str(base64.b64decode(_).decode('ascii')))
  21.     #print(", ".join(answers))
  22.     #1--------2-----3-----4--------5----6-----7------8
  23.     #1: list for decoded answers
  24.     #2: add it to the list
  25.     #3: string the final result to make it printable
  26.     #4: base64 module
  27.     #5: decode the thing we just got from the list
  28.     #6: the thing we just got from the list
  29.     #7/8: making it ascii removes the b''
  30.  
  31.  
  32. def wiyn(): #short for "What is your name?"
  33.     #print(", ".join(answers))
  34.     global wiyn #so we can destroy it in the go() function
  35.     global namev #so we can get the contents of the entry widget in the go() function
  36.     wiyn = tk.Tk() #init the tkinter window
  37.     wiyn.bind_all("<Return>", lambda _: go()) #make the enter key start the program as well as the start button
  38.     #1-------2---------3---------4---5----6
  39.     #1: Bind it to the tkinter window
  40.     #2: to bind
  41.     #3: what key to bind
  42.     #4: required
  43.     #5: throwaway variable, required
  44.     #6: go function
  45.  
  46.     namev = tk.StringVar() #make a tkinter string (because it is a name) variable
  47.     wiynlabel = tk.Label(wiyn, text='What is your name?', justify = 'center') #Make some text saying what to type into the box to help the user
  48.     wiynlabel.grid(row=0,column=0) #place it
  49.     nameentry = tk.Entry(wiyn, width=30, textvariable=namev).grid(row=1,column=0) #make the entry with the stringvar that we created earlier
  50.     b = tk.Button(wiyn, text="Start", command=go).grid(row=1,column=1) #make a button that calls the function "go", place it
  51.     wiyn.mainloop() #required
  52.  
  53. def go():
  54.     global e1,e2,e3,e4,e5,e6,e7,e8,e9,e10 #for .get() func in score checker
  55.     global en1,en2,en3,en4,en5 #for disable func in score checkr
  56.     global mkit #also for disable func in score checker
  57.     global name #so I can use it in other functions
  58.     name = namev.get() #get the contents of the stringvar that the entry box writes to
  59.     if name != "": #check that they actually entered a name
  60.         messagebox.showinfo("Welcome!", "Welcome, " + name + "!") #Welcome box!
  61.         wiyn.destroy() #destroy the what is your name window
  62.     else:
  63.         messagebox.showerror("Error!", "Please enter a name.") #inform the user that they didn't enter a name
  64.         sys.exit() #exit
  65.     root = tk.Tk() #start window
  66.     w = tk.Label(root, text='Welcome to Charlie\'s Pencil Quiz!\nRecommended for level 3 pencil enthusiasts.\nType in your answers and click \"Mark\" to mark it.', justify = 'center').grid(row=0,column=0)
  67.     #^ Info
  68.     for x in range(7): #7 times, writes questions on the screen
  69.         if x >= 6:
  70.             _ = tk.Label(root, text=questions[x],justify="left").grid(row=x+2,column=0)
  71.         else:
  72.             _ = tk.Label(root, text=questions[x],justify="left").grid(row=x+1,column=0)
  73.         #^------^-----^------------^------^----------------------------^
  74.         #1: throwaway var
  75.         #2: Label
  76.         #3: main window
  77.         #4: takes it from position x of the array "questions"
  78.         #5:
  79.  
  80.     e1 = tk.StringVar() #make the entry box variables
  81.     e2 = tk.StringVar()
  82.     e3 = tk.StringVar()
  83.     e4 = tk.StringVar()
  84.     e5 = tk.StringVar()
  85.    
  86.     en1 = tk.Entry(root, width=30, textvariable=e1) #make the entry boxes
  87.     en1.grid(row=1,column=1)
  88.     en2 = tk.Entry(root, width=30, textvariable=e2)
  89.     en2.grid(row=2,column=1)
  90.     en3 = tk.Entry(root, width=30, textvariable=e3)
  91.     en3.grid(row=3,column=1)
  92.     en4 = tk.Entry(root, width=30, textvariable=e4)
  93.     en4.grid(row=4,column=1)
  94.     en5 = tk.Entry(root, width=30, textvariable=e5) #adding .grid() here returned None
  95.     en5.grid(row=5,column=1)
  96.     e6 = tk.IntVar()
  97.     e7 = tk.IntVar()
  98.     e8 = tk.IntVar()
  99.     e9 = tk.IntVar()
  100.     e10 = tk.IntVar()
  101.     temp = ["3,000","30,000","300,000","3,000,000"]
  102.     temp2 = ["Nikon","Bentley","Staedtler","Intel"]
  103.     temp3 = [e7, e8, e9, e10]
  104.     for i in range(8):
  105.         if i <= 1:
  106.             tk.Radiobutton(root, text=temp[i], variable=e6, value=i+1).grid(row=6,column=i+1)
  107.         elif i <= 3:
  108.             tk.Radiobutton(root, text=temp[i], variable=e6, value=i+1).grid(row=7,column=i-1)
  109.         elif i <= 5:
  110.             tk.Checkbutton(root, text=temp2[i-4], variable=temp3[i-4]).grid(row=8,column=i-3)
  111.         else:
  112.             tk.Checkbutton(root, text=temp2[i-4], variable=temp3[i-4]).grid(row=9,column=i-5)
  113.            
  114.     mkit = tk.Button(root, text="Mark It!", command=markit, fg="red") #mark it button
  115.     mkit.grid(row=10,column=1)
  116.    
  117. def markit():
  118.     score = 0
  119.     mkit.config(state = "disabled") #disable entries and buttons
  120.     en1.config(state = "disabled")
  121.     en2.config(state = "disabled")
  122.     en3.config(state = "disabled")
  123.     en4.config(state = "disabled")
  124.     en5.config(state = "disabled")
  125.  
  126.     attempt.append(e1.get()) #append all attempts to the attempt list
  127.     attempt.append(e2.get())
  128.     attempt.append(e3.get())
  129.     attempt.append(e4.get())
  130.     attempt.append(e5.get())
  131.     attempt.append(str(e6.get()))
  132.     #print("--A--")
  133.     #print("\n".join(attempt))
  134.     #print("--/A--")
  135.     print(e7.get())
  136.     print(e8.get())
  137.     print(e9.get())
  138.     print(e10.get())
  139.    
  140.     for __ in range(7):
  141.        
  142.         #DEBUG
  143.         #print("--SD--")
  144.         #print("--1--")
  145.         #print(attempt[2])
  146.         #print("--2--")
  147.         #print(min(max(float(attempt[2]),1.7),1.9))
  148.         #print("--ED--")
  149.         #END DEBUG
  150.        
  151.         try:
  152.             if (__ == 2 and float(attempt[__]) == min(max(float(attempt[__]),1.7),1.9)): #1.7 < attempt < 1.9 = correct
  153.             #-------^--------^---(-----------------------------^-----------------)
  154.             #1) if the question is 3 (2 as for loops start with 0)
  155.             #2) float it so it doesn't return a valueerror if the user uses decimals
  156.             #3) if the maximum of the attempt and 1.7 is the attempt and the minimum of that and 1.9 is the attempt then the attempt is between 1.7 and 1.9
  157.  
  158.                 xd.append(str(__+1) + ": Correct") #add the number of the question and Correct to the list shown in the score popup
  159.                 score+=1 #add 1 to the score
  160.             elif __ == 6:
  161.                 if e7.get() == 0 and e8.get() == 1 and e9.get() == 1 and e10.get() == 0:
  162.                     xd.append("7: Correct") #add the number of the question and Correct to the list shown in the score popup
  163.                     score+=1 #add 1 to the score
  164.                 else:
  165.                     xd.append("7: Incorrect")
  166.             else: #if the question isn't 3
  167.                 if answers[__].lower() == attempt[__].lower(): #if the answer in the answers list is equal to the attempt (non-case sensitive)
  168.                     xd.append(str(__+1) + ": Correct") #add the number of the question and Correct to the list shown in the score popup
  169.                     score+=1 #add 1 to the score
  170.                 else: #if it's wrong
  171.                     xd.append(str(__+1) + ": Incorrect") #add the number of the question and Incorrect to the list shown in the score popup
  172.         except ValueError: #someone inputted a non-number for q3
  173.             print("valueerror")
  174.             xd.append(str(__+1) + ": Non-number inputted: Incorrect")#add the number of the question and Incorrect to the list shown in the score popup
  175.             pass #continue
  176.        
  177.     messagebox.showinfo(praise[score-1], "Your score: " + str(score) + ". " + praise[score-1] + "!" + "\n" + "\n".join(xd))
  178.     #----------------------^-------------------------------------------------------------------------------------^---------
  179.     #1: Tell them either "Bad", "OK", "Nice", "Well Done" or "Awful" from the praise list
  180.     #2: list the list with a newline between each item
  181.    
  182.     if score > 3: #if the score is more than 3
  183.         messagebox.showinfo("Well Done!", "Well done!\nWell done!\nWell done!\nWell done!\nWell done!") #5 well dones
  184.        
  185.  
  186.    
  187.  
  188.  
  189. wiyn() #start it
  190.  
  191.  
  192.  
  193. #-------------------------------
  194. #Here is a much simpler one
  195. #----------------------------
  196.  
  197. def simple():
  198.     answers = ["a", "a", "c", "b", "d"]
  199.     questions = ["When was the Mechanical pencil patented? a: 1822 b: 1922 c: 1722 d: 1622 ",
  200.                  "What is the largest pencil manufacturer? a: Faber-Castell b: Bentley c: Staedtler d: Berol ",
  201.                  "Around how many pencils does Faber-Castell make per year in billions? a: 1 b: 1.7 c: 1.8 d: 0.5 ",
  202.                  "Does Ferrari make pencils? a: yes b: no c: probably d: i don't know ",
  203.                  "What year were pencils invented? a: 1653 b: 1785 c: 200 BC d: 1795 "]
  204.     attempt = []
  205.     score = 0
  206.     for i in range(5):
  207.         attempt.append(input(questions[i]))
  208.         if attempt[i] == answers[i]:
  209.             score+=1
  210.     print(score)
  211.     if score > 3:
  212.         for k in range(5):
  213.             print("Well Done!")
  214.  
  215.  
  216.  
  217. ##root = tk.Tk()
  218. ##
  219. ##w = tk.Label(root, text='Temp', justify = 'center').pack()
  220. ##root.mainloop()
  221. #                print(str(attempt[__]) + " || " + str(min(max(float(attempt[__]),1.7),1.9)))                
  222. #            print("Iteration #" + str(__) + " | " + str(attempt[__]))
  223. #temp = ["1822", "Faber-Castell", "Filler", "No", "1795"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement