Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Gimme5 Arrays by Colin
- """
- 1. Here is a 1 dimensional (1D) array
- squad = ["Dave", "Daisy", "Donald", "Daphne", "Douglas", "Dovisia"]
- 1a. Predict and then Write some code that tells you how long squad is
- 1b. Write some code that tells you what name is at index 3
- 1c. What does the following piece of code do, and prove it.
- squad.sort()
- 2. Write some code that prints out the number of letters of each element.
- 3. Here is another array, this time it's 2 dimensional (2D)
- squadgoals = [["Dave",3], ["Daisy", 6], ["Donald", 2], ["Daphne", 6], ["Douglas",3], ["Dovisia",0]]
- 3a. Predict and then Write some code that tells you how long squadgoals is
- 3b. Write some code that tells you what is at index 5
- 3c. Predict then prove it what the following produces
- print(squadgoals[5][0])
- EXTENSION for Q3
- #3d. Codily add another name begining with D who scored 2 goals, prove it has been added
- squadgoals.append(["Delilah",2])
- print(squadgoals)
- 4a. Write some code that prints out the names of the people who scored 3 goals
- 4b. Write some code that prints out the maximum goals scored
- 5. Create a mini programme that asks people their name and their favourite pet, stored as an array.
- should loop until xxx is entered as a name, and then output the array
- """
- #1
- squad = ["Dave", "Daisy", "Donald", "Daphne", "Douglas", "Dovisia"]
- # 1a. Predict and then Write some code that tells you how long squad is
- # prediction 6
- print("squad has length of :-", len(squad))
- # 1b. Write some code that tells you what name is at index 3
- print("The element at index 3, forth item is ;-", squad[3])
- # 1c. What does the following piece of code do, and prove it.
- # squad.sort()
- print("before:-", squad)
- squad.sort()
- print("after:-", squad)
- #2
- for i in range(len(squad)):
- print(squad[i], "has length", len(squad[i]))
- #3
- squadgoals = [["Dave",3], ["Daisy", 6], ["Donald", 2], ["Daphne", 6], ["Douglas",3], ["Dovisia",0]]
- #3a. Predict and then Write some code that tells you how long squadgoals is
- # prediction 7
- print("squadgoals has length of :-", len(squadgoals))
- #3b. Write some code that tells you what is at index 5
- print("The element at index 5, sixth item is ;-", squadgoals[5])
- #3c. Predict then prove it what the following produces
- # print(squadgoals[5][0])
- #prediction = Dovisia
- print(squadgoals[5][0])
- #4
- #4a. Write some code that prints out the names of the people who scored 3 goals
- for i in range(len(squadgoals)):
- if squadgoals[i][1] == 3:
- print( squadgoals[i][0])
- #4b. Write some code that prints out the maximum goals scored
- max = 0
- for i in range(len(squadgoals)):
- if squadgoals[i][1] > max:
- max = squadgoals[i][1]
- print(max)
- # 5. Create a mini programme that asks people their name and their favourite pet type, stored as an array, it
- # should loop until xxx is entered as a name.
- pet_array = []
- name = ""
- while name != "xxx":
- name = input("Enter name >> ")
- if name != "xxx":
- favpet = input("Enter fav pet type >> ")
- pet_array.append([name,favpet])
- print(pet_array)
Add Comment
Please, Sign In to add comment