- Exercise 1:
- Create a function called “starwars”, no parameter.
- Create a new variable (“robots”) with datatype List of String.
- The value of this”robots” variable is: “R2-D2”, “C-3PO”, “ASN-121”.
- When this function is called, it will print all of the elements inside the list. Use “for loop” to go through each element inside the list.
R2-D2
C-3PO
ASN-121
- Exercise 2:
- Create a function called “starwars2”. no parameter.
- Create a new variable (“robots”) with datatype List of String.
- The value of this “robots” variable is: “R2-D2”, “C-3PO”, “ASN-121”, “WALL-E”, “DATA”, “BUMBLE-BEE”.
- Using random module, choose a random element from the list. print this random element to the shell.
- Exercise 3:
- Create a function called “starwars3”, one parameter (“suffix”)
- Copy the code from “starwars2” function into “starwars3” function.
- Instead of just printing the robot name, combine the robot name with the parameter “suffix”
- For example, if a user calls the function like the following
- Call the function like this
starwars3(" is the best")
starwars3(" is my friend")
- And if the computer pick “C-3PO” for the first call and the second call the computer pick “R2-D2”, then in the shell, it will look like this
C-3PO is the best
R2-D2 is my friend
- Create a function “starwars_weapon”. one parameter, “type”
- if “type” parameter is “small”, the function will return one of these weapons randomly: light saber, blaster pistol, sonic pistol
- if “type” parameter is “big”, the function will return one of these weapons randomly: blaster cannon, missile launcher, plasma cannon.
- call the function, get the return value and print to the shell.