2.18. Chapter Assessment¶
Check your understanding
There is a function we are providing in for you in this problem called
square. It takes one integer and returns the square of that integer value. Write code to assign a variable called
xyz the value
5*5 (five squared). Use the square function, rather than just multiplying with
Write code to assign the number of characters in the string
rv to a variable
- a = len("hello worldwelcome!")
- Though we are using the len function here, we are hardcoding what len should return the length of. We are not referencing z or y.
- a = 11 + 8
- This is hardcoding, we are writing in the value without referencing z or y.
- a = len(z) + len(y)
- This is not considered hard coding. We are using the function len to determine the length of what is stored in z and y, which is a correct way to approach this problem.
- a = len("hello world") + len("welcome!")
- Though we are using the len function here, we are hardcoding what len should return the length of each time we call len. We are not referencing z or y.
- none of the above are hardcoding.
- At least one of these solutions is considered hardcoding. Take another look.
The code below initializes two variables,
y. We want to assign the total number of characters in
z and in
y to the variable
a. Which of the following solutions, if any, would be considered hard coding?
z = "hello world" y = "welcome!"
(This is not an assessment question) The code below defines functions used by one of the questions above. Do not modify the code, but feel free to take a look.