Conditionals Worksheet

Back to Week 4 Notes

Problem 1

Write a function even_or_odd that accepts an integer as an argument and returns a string indicating whether that argument is:

  • 'Even.'
  • 'Odd.'
  • 'Neither.' (if it is zero)
def even_or_odd(  ): # start by adding an argument
  # Your code here
assert even_or_odd(3) == "Odd."
assert even_or_odd(-1) == "Odd."
assert even_or_odd(1) == "Odd."
assert even_or_odd(0) == "Neither."
assert even_or_odd(4) == "Even."

Problem 2

Write a function greater_or_less_than that takes an int argument and returns whether the number is less than zero or greater than 10.

Examples:

  • greater_or_less_than(0) returns: "The number is not less than 0 or greater than 10."
  • greater_or_less_than(12) returns: "The number is less than 0 or greater than 10."
def greater_or_less_than(  ): # add an argument!
  # Your code here
assert greater_or_less_than(-1) == "The number is less than 0 or greater than 10."
assert greater_or_less_than(0) == "The number is not less than 0 or greater than 10."
assert greater_or_less_than(1) == "The number is not less than 0 or greater than 10."
assert greater_or_less_than(9) == "The number is not less than 0 or greater than 10."
assert greater_or_less_than(11) == "The number is less than 0 or greater than 10."

Problem 3

Write a function approximately that takes an int or float argument and returns the number to one significant figure:

Examples:

  • 12 rounded to one significant figure is 10
  • 123 rounded to one significant figure is 100
  • 5.22 rounded to one significant figure is 5
  • 23.8 rounded to one significant figure is 20

Assume the number will always be greater than 1 and less than 1000.

Hint: 215 % 100 yields 15 and 215 // 100 yields 2

def aproximately(  ): # check the function name and add an argument
  # Your code here
assert approximately(123) == 100
assert approximately(12) == 10
assert approximately(15.1) == 10
assert approximately(2.2) == 2
assert approximately(999) == 900

Problem 4

Write a function multiplier that takes an integer argument. If the number is positive, it should return the number multiplied by two. Otherwise, return the number multiplied by three.

def multiplier():
  # Your code here
assert multiplier(-10) == -30
assert multiplier(-1) == -3
assert multiplier(0) == 0
assert multiplier(1) == 2
assert multiplier(10) == 20

Problem 5

Write a function absolutely that takes an argument of any type:

  • If the argument is a number (int or float), return the absolute value of the argument
  • Otherwise, return the argument

Examples:

  • absolutely(2) returns 2
  • absolutely(-3.1) retuns 3.1
  • absolutely("what") returns string 'what'
# define your function
assert absolutely(3) == 3
assert absolutely(-2.5) == 2.5
assert absolutely(0) == 0
assert absolutely(2.1) == 2.1
assert absolutely(-1000) == 1000
assert absolutely(False) == False
assert absolutely("string arg") == "string arg"
assert absolutely(None) == None

Problem 6

Write a function round_loss that takes a positive float argument and returns how much is “lost” by rounding to the nearest int:

  • 4.2 rounds to 4, so .2 is “lost”
  • 1.6 rounds to 2, so .4 is “lost”
This is a hard problem.

Do this without any built-in rounding functions.

Recall how we truncated numbers: you’ll need to do something like this to to ‘fix’ the floating point error.

# define your function!
assert round_loss(1.25) == 0.25
assert round_loss(1) == 0
assert round_loss(4.65) == 0.35
assert round_loss(19.2) == 0.2
assert round_loss(12.99) == 0.01

Problem 7

Write a function greater_squares that accepts two integer arguments and returns the argument whose square is larger. If the squares are equal, return the greater argument.

(Recall that negative numbers, squared, yield positive numbers. \(-3^2\) is greater than \(2^2\))

def greater_squares(first_arg, second_arg):
  # Your code here
assert greater_squares(2, 5) == 5
assert greater_squares(-8, 8) == 8
assert greater_squares(-2.25, 2) == -2.25
assert greater_squares(-7, 2) == -7

Back to Week 4 Notes