I was searching the internet for some Python exercises, and I've found a lab assignment from University of Toronto. Here's the major of it:
Question 1.
Welcome Message In the if__name__ == "__main__"
block, write code that displays the following: Welcome to the calculator program. Current value: 0Question 2.
Displaying the Current Value Write a function whose signature isdisplay_current_value()
, and which displays the current value in the calculator. In the if__name__ == "__main__"
block, test this function by calling it and observing the output.Question 3.
Addition Write a function whose signature isadd(to_add)
, and which addsto_add
to the current value in the calculator, and modifies the current value accordingly. In the if__name__ == "__main__"
block, test the function add by calling it, as well as by callingdisplay_current_value()
. Hint: when modifying global variables from within functions, remember to declare them as global.Question 4.
Multiplication Write a function whose signature ismult(to_mult)
, and which multiplies the current value in the calculator byto_mult
, and modifies the current value accordingly. In the if__name__ == "__main__"
block, test the function.Question 5.
Division Write a function whose signature isdiv(to_div)
, and which divides the current value in the calculator byto_div
, and modifies the current value accordingly. In the if__name__ == "__main__"
block, test the function. What values ofto_div
might cause problems? Try them to see what happens.Question 6.
Memory and Recall Pocket calculators usually have a memory and a recall button. The memory button saves the current value and the recall button restores the saved value. Implement this functionality.Question 7.
Undo Implement a function that simulates the Undo button: the function restores the previous value that appeared on the screen before the current one.
Here's my solution:
current_value = 0
memory = current_value
memo = [current_value]
def is_divisible(x, y):
"""Return whether x is divisible by y. """
return x % y == 0
def update_list(lst, value):
"""Updates a list with an item. """
lst.append(value)
def display_current_value():
"""Prints the current value of the calculator. """
print('Current value:', current_value)
def add(to_add):
"""Adds a number to the current value of the calcalutor. """
global current_value
current_value += to_add
update_list(memo, current_value)
def mult(to_mult):
"""Multiplies the current value of the calcalutor by a number. """
global current_value
current_value *= to_mult
update_list(memo, current_value)
def div(to_div):
"""Divides the current value of the calcalutor by a number. """
global current_value
if is_divisible(current_value, to_div):
current_value //= to_div
else:
current_value /= to_div
update_list(memo, current_value)
def store():
"""Stores the current value. """
global memory
memory = current_value
def recall():
"""Recalls the saved value. """
return memory
def undo():
"""Restores the previous value. """
global memo
if len(memo) >= 2:
global current_value
current_value = memo[-2]
memo.pop(-2)
def main():
print('Welcome to the calculator program.' )
display_current_value()
add(6)
display_current_value()
mult(2)
display_current_value()
div(3)
display_current_value()
for iteration in range(3):
undo()
display_current_value()
if __name__ == '__main__':
main()
Is it possible to implement undo
without dealing with other functions such as add
? Is the code well-documented?
Please point out any bad practice or any bug I've made. Please point out how somethings can be done better. And please note that I'm a beginner and a hobbyist.
is_divisible(),
you want to check thaty != 0
, or catchZeroDivisionError
exception. \$\endgroup\$globals
. You can have the same behaviour, and a more robust code, if you send the value in the global to the function, and assign the result of the function. \$\endgroup\$