Unit 1: Extra Practice Problems

These are extra practice problems. Understanding how to solve all of these problems is good preparation for the miderm exam!

Extra Practice 1

Extra Practice 1

Write a function that takes one argument, a float.

Print, all one one line, with single spaces in between: the float rounded down to the nearest int, the float itself, and then the float rounded up to the nearest int.

Calling the function with argument 3.2 should print:

3 3.2 4

Calling the function with argument 5.7 should print:

5 5.7 6

Calling the function with argument 2.0 should print:

2 2.0 3

Try using only one call to print(), using string concatentation.

Extra Practice 2

Extra Practice 2

The program below was meant to print out:

- Books You Haven't Read
- Books You Needn't Read
- Books Made for Purposes Other Than Reading
- Books Read Even Before You Open Them
print('- Books You Haven't Read\n', end="")
print('- Books You Needn't Read')
print('-Books Made for Purposes Other Than Reading\n')
print('- Books Read Even Before,' end="")
print('You Open Them')

Fix the program so that it prints out the desired output. Try to make as few changes as possible, and understand what went wrong with the original program.

Extra Practice 3

Extra Practice 3

Write a function vessel that takes one argument.

If the argument is an int or a float: return three times the argument (and print nothing)

If the argument is a string: print out the argument three times, once each on a new line (and return nothing)

  • vessel(3) prints out nothing
  • print(vessel(4)) prints out 12
  • print(vessel(1.5)) prints out 4.5
  • vessel("Ross") prints out:
Ross
Ross
Ross
  • print(vessel("three")) prints out:
three
three
three
None

(The last None is from printing the function’s return)

Extra Practice 4

Extra Practice 4

Write a function embody that takes one argument, an int or a float, and returns a string consisting of: the argument, the argument multiplied by 2, and the argument divided by 2.

  • embody(5) returns string '5 10 2.5'
  • embody(5.0) returns string '5.0 10.0 2.5'
  • embody(2) returns string '2 4 1.0'

Calls to embody shouldn’t print anything.

Extra Practice 5

Extra Practice 5

Write a function sandwiches that takes one argument, which will be a two-digit integer.

Return the sum of the integer’s digits.

  • sandwiches(12) returns 3
  • sandwiches(45) returns 9
  • sandwiches(56) returns 11
  • sandwiches(20) returns 2

Hint: Use // and %.