Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. """
  2. CSCI 1200
  3. Lab Exercise 6.0
  4.  
  5. DICTIONARIES
  6.  
  7. In this lab we will learn how dictionaries use keys to access
  8. values. Do all work in Geany IDE and follow instructions provided
  9. in this code file.
  10.  
  11. Learning Outcomes
  12. =================
  13. In this lab you will learn how to:
  14. - declare dictionaries
  15. - access dictionary values
  16. - loop over dictionary items
  17.  
  18.  
  19. Evaluation
  20. ==========
  21. 20 pts. - submitted filename 6_0.py
  22. 20 pts. - 6_0.py executes without error
  23. 20 pts. - Part 1 meets requirements
  24. 40 pts. - Part 2 meets requirements
  25.  
  26.  
  27. Submission
  28. ==========
  29. As before, submit your work on D2L or using git.
  30. Earn 5 pts. extra credit by submitting using git by due date.
  31.  
  32. """
  33.  
  34.  
  35. #=======================================================================
  36. # PART 1
  37. #-----------------------------------------------------------------------
  38. ''' DO NOT EDIT '''
  39. import random
  40. num_doors = 6
  41. seed = random.randint(0, num_doors-1)
  42. val = [1 if x == seed else 0 for x in range(0, num_doors)]
  43. doors = dict(zip(list('ABCDEF'), val))
  44. ''' END '''
  45.  
  46.  
  47. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  48. BACKGROUND
  49. In part 1 we will practice the idea how dictionaries use *keys*
  50. to access *values*. Imagine in front of you are 6 doors:
  51.  
  52. ___ ___ ___ ___ ___ ___
  53. | | | | | | | | | | | |
  54. | A | | B | | C | | D | | E | | F |
  55. | | | | | | | | | | | |
  56. -----------------------------------------------
  57.  
  58. Each door is locked and you must use a key to see what is behind
  59. the door. Behind 1 of the doors is a surprise. This special door
  60. will change places every time you run the program.
  61.  
  62. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  63.  
  64. # Your task is to write code that reveals the door that contains
  65. # the suprise. Do as follows:
  66.  
  67. # 1. Write a for loop where you go through each dictionary item.
  68. # The name of the dictionary is ‘doors’
  69. #
  70. # 2. Access the value behind each door.
  71. # If the value is 1 you have found the suprise and you should
  72. # print: "Jackpot! Surprise is behind door X" where X is the
  73. # door key. If the value is not 1, print: Not behind door X.
  74. #
  75. # Hint: it may help to run print(doors) a few times to see what the
  76. # dictionary looks like and how the special door changes place each time
  77.  
  78. print(doors)
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  89. print()
  90. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  91. #=======================================================================
  92. # PART 2
  93. #-----------------------------------------------------------------------
  94. #
  95. # In this part we will practice declaring dictionaries and
  96. # adding values to it. In fact, let's create and work with a dictionary
  97. # of dictionaries!
  98. #
  99. # Your mission in this lab exercise is to create a dictionary of three
  100. # cities. Each city will itself be a dictionary. In essence, you're
  101. # creating a dictionary of dictionaries.
  102. #
  103. # After creating the dictionary of dictionaries, write out the city
  104. # names and all city info.
  105. #-----------------------------------------------------------------------
  106.  
  107.  
  108.  
  109. # 1. Create a dictionary named ‘cities’. Select the names of three U.S.
  110. # cities (your choice) as keys in your dictionary.
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. # 2. Create a dictionary of information about each city.
  120. # Include the:
  121. # 1) county the city is in
  122. # 2) state the city is in
  123. # 3) population of the city
  124. # Use online sources to obtain the county and population information.
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. # 3. After creating the dictionary, print the name of each city and all
  134. # the information you have stored about that city. See this example:
  135. # https://i.imgur.com/UhEFrEV.png
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. #=======================================================================
  144. # That's it for today!
  145. # End of Lab Exercise 6.0
  146. #=======================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement