Conditionals Worksheet
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 hereassert 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 hereassert 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:
12rounded to one significant figure is10123rounded to one significant figure is1005.22rounded to one significant figure is523.8rounded to one significant figure is20
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 hereassert approximately(123) == 100
assert approximately(12) == 10
assert approximately(15.1) == 10
assert approximately(2.2) == 2
assert approximately(999) == 900Problem 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 hereassert multiplier(-10) == -30
assert multiplier(-1) == -3
assert multiplier(0) == 0
assert multiplier(1) == 2
assert multiplier(10) == 20Problem 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)returns2absolutely(-3.1)retuns3.1absolutely("what")returns string'what'
# define your functionassert 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) == NoneProblem 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.2rounds to4, so.2is “lost”1.6rounds to2, so.4is “lost”
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.01Problem 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 hereassert 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