Exercise 02 - CS61A fa2022
Fortune Teller
"""
Why pay a fortune teller when you can just program your fortune yourself?
Write a function named tell_fortune that:
* takes 3 arguments: job title, partner's name, geographic location.
* returns a fortune of the form: 'You will be a {job_title} in {location} living with {partner}.
"""
def tell_fortune(job_title, location, partner):
"""
>>> tell_fortune('bball player', 'Spain', 'Shaqira')
'You will be a bball player in Spain living with Shaqira.'
>>> tell_fortune('farmer', 'Kansas', 'C3PO')
'You will be a farmer in Kansas living with C3PO.'
>>> tell_fortune('Elvis Impersonator', 'Russia', 'Karl the Fog')
'You will be a Elvis Impersonator in Russia living with Karl the Fog.'
"""
return f"You will be a {job_title} in {location} living with {partner}."
print(tell_fortune('bball player', 'Spain', 'Shaqira'))
Operator Expression
from operator import add, sub, mul, truediv, floordiv, mod
"""
In the console, type expressions that use function calls
and correspond to each arithmetic expression.
Use the console to check your work.
Useful documentation:
https://docs.python.org/3/library/operator.html#mapping-operators-to-functions
"""
# Q1: 30 + 2
print(add(30,2))
# Q2: 30 - 2
print(sub(30,2))
# Q3: 30 * 2
print(mul(30,2))
# Q4: 30 / 2
print(truediv(30, 2))
# Q5: 30 % 2
print(mod(30, 2))
# Nested!
# Q6: 30 + (2 * 4)
print(add(20,mul(2,4)))
# Q7: 3 * (10 - 2)
print(mul(3,sub(10,2)))
# Challenge!
# Q8: (3 ** (365/52)) - 1
print(sub(pow(3, truediv(365, 52)), 1))
# Q9: (25 // 4) - (25 / 4)
print(sub(floordiv(25, 4), (sub(25, 4))))