Advertisement
ukesh

quiz

Feb 23rd, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #written by usingpython.com
  2. #allows us to access a random 'key' in the dictionary
  3. import random
  4. #the questions/answer dictionary
  5. my_dict = {
  6. "Base-2 number system" : "binary",
  7. "Number system that uses the characters 0-F" : "hexidecimal",
  8. "7-bit text encoding standard" : "ascii",
  9. "16-bit text encoding standard" : "unicode",
  10. "A number that is bigger than the maximum number that can be stored" : "overflow",
  11. "8 bits" : "byte",
  12. "1024 bytes" : "kilobyte",
  13. "Picture Element. The smallest component of a bitmapped image" : "pixel",
  14. "A continuously changing wave, such as natural sound" : "analogue",
  15. "the number of times per second that a wave is measured" : "sample rate",
  16. "A bunary representation of a program" : "machine code"
  17. }
  18. #welcome message
  19. print("Computing Revision Quiz")
  20. print("=======================")
  21. #the quiz will end when this variable becomes 'False'
  22. playing = True
  23. #While the game is running
  24. while playing == True:
  25. #set the score to 0
  26. score = 0
  27. #gets the number of questions the player wants to answer
  28. num = int(input("\nHow many questions would you like: "))
  29. #loop the correct number of times
  30. for i in range(num):
  31. #the question is one of the dictionary keys, picked at random
  32. question = (random.choice( list(my_dict.keys())))
  33. #the answer is the string mapped to the question key
  34. answer = my_dict[question]
  35. #print the question, along with the question number
  36. print("\nQuestion " + str(i+1) )
  37. print(question + "?")
  38. #get the user's answer attempt
  39. guess = input("> ")
  40. #if their guess is the same as the answer
  41. if guess.lower() == answer.lower():
  42. #add 1 to the score and print a message
  43. print("Correct!")
  44. score += 1
  45. else:
  46. print("Nope!")
  47. #after the quiz, print their final score
  48. print("\nYour final score was " + str(score))
  49. #store the user's input...
  50. again = input("Enter any key to play again, or 'q' to quit.")
  51. #... and quit if they types 'q'
  52. if again.lower() == 'q':
  53. playing = Fals e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement