timber101

Gimme5 - Array

Jan 19th, 2022 (edited)
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. #  Gimme5 Arrays by Colin
  2. """
  3. 1. Here is a 1 dimensional (1D) array
  4.  
  5. squad = ["Dave", "Daisy", "Donald", "Daphne", "Douglas", "Dovisia"]
  6.  
  7. 1a.  Predict and then Write some code that tells you how long squad is
  8. 1b.  Write some code that tells you what name is at index 3
  9. 1c.  What does the following piece of code do, and prove it.
  10.        squad.sort()
  11.        
  12. 2.  Write some code that prints out the number of letters of each element.
  13.  
  14. 3.  Here is another array, this time it's 2 dimensional (2D)
  15. squadgoals = [["Dave",3], ["Daisy", 6], ["Donald", 2], ["Daphne", 6], ["Douglas",3], ["Dovisia",0]]
  16.  
  17. 3a.  Predict and then Write some code that tells you how long squadgoals is
  18. 3b.  Write some code that tells you what is at index 5
  19. 3c.  Predict then prove it what the following produces
  20.        print(squadgoals[5][0])
  21. EXTENSION for Q3
  22. #3d.  Codily add another name begining with D who scored 2 goals, prove it has been added
  23. squadgoals.append(["Delilah",2])
  24. print(squadgoals)
  25.  
  26. 4a.  Write some code that prints out the names of the people who scored 3 goals
  27. 4b.  Write some code that prints out the maximum goals scored
  28.  
  29. 5.  Create a mini programme that asks people their name and their favourite pet, stored as an array.
  30. should loop until xxx is entered as a name, and then output the array
  31.  
  32.  
  33. """
  34.  
  35. #1
  36.  
  37. squad = ["Dave", "Daisy", "Donald", "Daphne", "Douglas", "Dovisia"]
  38.  
  39. #  1a.  Predict and then Write some code that tells you how long squad is
  40. # prediction 6
  41. print("squad has length of :-", len(squad))
  42. #  1b.  Write some code that tells you what name is at index 3
  43. print("The element at index 3, forth item is ;-", squad[3])
  44. #  1c.  What does the following piece of code do, and prove it.
  45. #        squad.sort()
  46. print("before:-", squad)
  47. squad.sort()
  48. print("after:-", squad)
  49.  
  50.  
  51. #2
  52. for i in range(len(squad)):
  53.     print(squad[i], "has length", len(squad[i]))
  54.    
  55.    
  56. #3
  57. squadgoals = [["Dave",3], ["Daisy", 6], ["Donald", 2], ["Daphne", 6], ["Douglas",3], ["Dovisia",0]]
  58.  
  59. #3a. Predict and then Write some code that tells you how long squadgoals is
  60. # prediction 7
  61. print("squadgoals has length of :-", len(squadgoals))
  62. #3b.  Write some code that tells you what is at index 5
  63. print("The element at index 5, sixth item is ;-", squadgoals[5])
  64. #3c.  Predict then prove it what the following produces
  65. #        print(squadgoals[5][0])
  66. #prediction = Dovisia
  67. print(squadgoals[5][0])
  68.  
  69.  
  70. #4
  71. #4a.  Write some code that prints out the names of the people who scored 3 goals
  72. for i in range(len(squadgoals)):
  73.     if squadgoals[i][1] == 3:
  74.         print( squadgoals[i][0])
  75.  
  76. #4b.  Write some code that prints out the maximum goals scored
  77. max = 0        
  78. for i in range(len(squadgoals)):
  79.     if squadgoals[i][1] > max:
  80.         max = squadgoals[i][1]
  81.  
  82. print(max)
  83.  
  84.  
  85. # 5.  Create a mini programme that asks people their name and their favourite pet type, stored as an array, it
  86. # should loop until xxx is entered as a name.
  87.  
  88. pet_array = []
  89. name = ""
  90.  
  91. while name != "xxx":
  92.     name = input("Enter name >> ")
  93.     if name != "xxx":
  94.         favpet = input("Enter fav pet type >> ")
  95.         pet_array.append([name,favpet])
  96.        
  97. print(pet_array)
  98.  
Add Comment
Please, Sign In to add comment