Skip to main content

Foundations of Python Programming: Functions First

Section 2.17 Chapter Assessment

Check your understanding
Listing 2.17.1.
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 *.

Checkpoint 2.17.2.

Write code to assign the number of characters in the string rv to a variable num_chars.

Checkpoint 2.17.3.

    The code below initializes two variables, z and 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!"
    
  • 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.
You have attempted of activities on this page.