Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk #module for window
- from tkinter import messagebox #module for message boxes
- import base64 #module for decoding answers
- import sys
- score = 0
- questions = ["When was the Mechanical pencil patented?",
- "What is the largest pencil manufacturer?",
- "Around how many pencils does Faber-Castell make per year in billions?",
- "Does Ferrari make pencils?",
- "What year were pencils invented?",
- "How many pencils can a single tree make?",
- "Which of these are pencil maunfacturers?"]
- answerse = [b'MTgyMg==',b'RmFiZXItQ2FzdGVsbA==',b'RmlsbGVy',b'Tm8=',b'MTc5NQ==',b'Mw==',b'RmlsbGVy'] #encoded answers
- q6options = [b"MywwMDANCg==",b"MzAsMDAwDQo=",b"MzAwLDAwMA0K",b"MywwMDAsMDAwDQo="]
- answers = [] #list for decoded answers
- attempt = [] #list for attempt
- xd = []
- praise = ["Awful","Bad", "OK", "Nice", "Well Done","Amazing","Perfect"] #In score show-er box
- for _ in answerse: #for every item in the encoded answers list
- answers.append(str(base64.b64decode(_).decode('ascii')))
- #print(", ".join(answers))
- #1--------2-----3-----4--------5----6-----7------8
- #1: list for decoded answers
- #2: add it to the list
- #3: string the final result to make it printable
- #4: base64 module
- #5: decode the thing we just got from the list
- #6: the thing we just got from the list
- #7/8: making it ascii removes the b''
- def wiyn(): #short for "What is your name?"
- #print(", ".join(answers))
- global wiyn #so we can destroy it in the go() function
- global namev #so we can get the contents of the entry widget in the go() function
- wiyn = tk.Tk() #init the tkinter window
- wiyn.bind_all("<Return>", lambda _: go()) #make the enter key start the program as well as the start button
- #1-------2---------3---------4---5----6
- #1: Bind it to the tkinter window
- #2: to bind
- #3: what key to bind
- #4: required
- #5: throwaway variable, required
- #6: go function
- namev = tk.StringVar() #make a tkinter string (because it is a name) variable
- 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
- wiynlabel.grid(row=0,column=0) #place it
- nameentry = tk.Entry(wiyn, width=30, textvariable=namev).grid(row=1,column=0) #make the entry with the stringvar that we created earlier
- b = tk.Button(wiyn, text="Start", command=go).grid(row=1,column=1) #make a button that calls the function "go", place it
- wiyn.mainloop() #required
- def go():
- global e1,e2,e3,e4,e5,e6,e7,e8,e9,e10 #for .get() func in score checker
- global en1,en2,en3,en4,en5 #for disable func in score checkr
- global mkit #also for disable func in score checker
- global name #so I can use it in other functions
- name = namev.get() #get the contents of the stringvar that the entry box writes to
- if name != "": #check that they actually entered a name
- messagebox.showinfo("Welcome!", "Welcome, " + name + "!") #Welcome box!
- wiyn.destroy() #destroy the what is your name window
- else:
- messagebox.showerror("Error!", "Please enter a name.") #inform the user that they didn't enter a name
- sys.exit() #exit
- root = tk.Tk() #start window
- 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)
- #^ Info
- for x in range(7): #7 times, writes questions on the screen
- if x >= 6:
- _ = tk.Label(root, text=questions[x],justify="left").grid(row=x+2,column=0)
- else:
- _ = tk.Label(root, text=questions[x],justify="left").grid(row=x+1,column=0)
- #^------^-----^------------^------^----------------------------^
- #1: throwaway var
- #2: Label
- #3: main window
- #4: takes it from position x of the array "questions"
- #5:
- e1 = tk.StringVar() #make the entry box variables
- e2 = tk.StringVar()
- e3 = tk.StringVar()
- e4 = tk.StringVar()
- e5 = tk.StringVar()
- en1 = tk.Entry(root, width=30, textvariable=e1) #make the entry boxes
- en1.grid(row=1,column=1)
- en2 = tk.Entry(root, width=30, textvariable=e2)
- en2.grid(row=2,column=1)
- en3 = tk.Entry(root, width=30, textvariable=e3)
- en3.grid(row=3,column=1)
- en4 = tk.Entry(root, width=30, textvariable=e4)
- en4.grid(row=4,column=1)
- en5 = tk.Entry(root, width=30, textvariable=e5) #adding .grid() here returned None
- en5.grid(row=5,column=1)
- e6 = tk.IntVar()
- e7 = tk.IntVar()
- e8 = tk.IntVar()
- e9 = tk.IntVar()
- e10 = tk.IntVar()
- temp = ["3,000","30,000","300,000","3,000,000"]
- temp2 = ["Nikon","Bentley","Staedtler","Intel"]
- temp3 = [e7, e8, e9, e10]
- for i in range(8):
- if i <= 1:
- tk.Radiobutton(root, text=temp[i], variable=e6, value=i+1).grid(row=6,column=i+1)
- elif i <= 3:
- tk.Radiobutton(root, text=temp[i], variable=e6, value=i+1).grid(row=7,column=i-1)
- elif i <= 5:
- tk.Checkbutton(root, text=temp2[i-4], variable=temp3[i-4]).grid(row=8,column=i-3)
- else:
- tk.Checkbutton(root, text=temp2[i-4], variable=temp3[i-4]).grid(row=9,column=i-5)
- mkit = tk.Button(root, text="Mark It!", command=markit, fg="red") #mark it button
- mkit.grid(row=10,column=1)
- def markit():
- score = 0
- mkit.config(state = "disabled") #disable entries and buttons
- en1.config(state = "disabled")
- en2.config(state = "disabled")
- en3.config(state = "disabled")
- en4.config(state = "disabled")
- en5.config(state = "disabled")
- attempt.append(e1.get()) #append all attempts to the attempt list
- attempt.append(e2.get())
- attempt.append(e3.get())
- attempt.append(e4.get())
- attempt.append(e5.get())
- attempt.append(str(e6.get()))
- #print("--A--")
- #print("\n".join(attempt))
- #print("--/A--")
- print(e7.get())
- print(e8.get())
- print(e9.get())
- print(e10.get())
- for __ in range(7):
- #DEBUG
- #print("--SD--")
- #print("--1--")
- #print(attempt[2])
- #print("--2--")
- #print(min(max(float(attempt[2]),1.7),1.9))
- #print("--ED--")
- #END DEBUG
- try:
- if (__ == 2 and float(attempt[__]) == min(max(float(attempt[__]),1.7),1.9)): #1.7 < attempt < 1.9 = correct
- #-------^--------^---(-----------------------------^-----------------)
- #1) if the question is 3 (2 as for loops start with 0)
- #2) float it so it doesn't return a valueerror if the user uses decimals
- #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
- xd.append(str(__+1) + ": Correct") #add the number of the question and Correct to the list shown in the score popup
- score+=1 #add 1 to the score
- elif __ == 6:
- if e7.get() == 0 and e8.get() == 1 and e9.get() == 1 and e10.get() == 0:
- xd.append("7: Correct") #add the number of the question and Correct to the list shown in the score popup
- score+=1 #add 1 to the score
- else:
- xd.append("7: Incorrect")
- else: #if the question isn't 3
- if answers[__].lower() == attempt[__].lower(): #if the answer in the answers list is equal to the attempt (non-case sensitive)
- xd.append(str(__+1) + ": Correct") #add the number of the question and Correct to the list shown in the score popup
- score+=1 #add 1 to the score
- else: #if it's wrong
- xd.append(str(__+1) + ": Incorrect") #add the number of the question and Incorrect to the list shown in the score popup
- except ValueError: #someone inputted a non-number for q3
- print("valueerror")
- xd.append(str(__+1) + ": Non-number inputted: Incorrect")#add the number of the question and Incorrect to the list shown in the score popup
- pass #continue
- messagebox.showinfo(praise[score-1], "Your score: " + str(score) + ". " + praise[score-1] + "!" + "\n" + "\n".join(xd))
- #----------------------^-------------------------------------------------------------------------------------^---------
- #1: Tell them either "Bad", "OK", "Nice", "Well Done" or "Awful" from the praise list
- #2: list the list with a newline between each item
- if score > 3: #if the score is more than 3
- messagebox.showinfo("Well Done!", "Well done!\nWell done!\nWell done!\nWell done!\nWell done!") #5 well dones
- wiyn() #start it
- #-------------------------------
- #Here is a much simpler one
- #----------------------------
- def simple():
- answers = ["a", "a", "c", "b", "d"]
- questions = ["When was the Mechanical pencil patented? a: 1822 b: 1922 c: 1722 d: 1622 ",
- "What is the largest pencil manufacturer? a: Faber-Castell b: Bentley c: Staedtler d: Berol ",
- "Around how many pencils does Faber-Castell make per year in billions? a: 1 b: 1.7 c: 1.8 d: 0.5 ",
- "Does Ferrari make pencils? a: yes b: no c: probably d: i don't know ",
- "What year were pencils invented? a: 1653 b: 1785 c: 200 BC d: 1795 "]
- attempt = []
- score = 0
- for i in range(5):
- attempt.append(input(questions[i]))
- if attempt[i] == answers[i]:
- score+=1
- print(score)
- if score > 3:
- for k in range(5):
- print("Well Done!")
- ##root = tk.Tk()
- ##
- ##w = tk.Label(root, text='Temp', justify = 'center').pack()
- ##root.mainloop()
- # print(str(attempt[__]) + " || " + str(min(max(float(attempt[__]),1.7),1.9)))
- # print("Iteration #" + str(__) + " | " + str(attempt[__]))
- #temp = ["1822", "Faber-Castell", "Filler", "No", "1795"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement