Feb-15-2020, 12:29 AM
This is for a personal challenge, not for school.
The task at hand:
Write a function which collects a list of floats as well as a boolean option. When the option is set to
Here is my script that I came up with:
The problem is clearly with how I am referencing the
So I Google 'UnboundLocalError' which turns up a doc by the UCSB titled:"Error: UnboundLocalError: local variable 'num' referenced before assignment". Based on my reading of this guide, I tried replacing line 13 with:
What kind of hints or advice could you people provide without giving me the solution entirely?
By the way, I originally got the idea to use the
The task at hand:
Write a function which collects a list of floats as well as a boolean option. When the option is set to
True
, round up to the nearest full integer. However when the option is set to False
, round down.Here is my script that I came up with:
from math import ceil, floor list_of_floats = [1.23, 4.32, 4.96, 16.10, 16.987] rounded_list_of_floats = [] option = True def round_up_or_down(list_of_floats, option): for individual_float in list_of_floats: if option == True: rounded_list_of_floats = rounded_list_of_floats + \ ceil(individual_float) if option == False: rounded_list_of_floats = rounded_list_of_floats + \ floor(individual_float) return rounded_list_of_floats print(round_up_or_down(list_of_floats, False))My interpreter runs this trace-back:
Quote:› python Bite153.py
Traceback (most recent call last):
File "Bite153.py", line 21, in <module>
print(round_up_or_down(list_of_floats, False))
File "Bite153.py", line 16, in round_up_or_down
rounded_list_of_floats = rounded_list_of_floats + \
UnboundLocalError: local variable 'rounded_list_of_floats' referenced before assignment
The problem is clearly with how I am referencing the
rounded_list_of_floats
variable. So I Google 'UnboundLocalError' which turns up a doc by the UCSB titled:"Error: UnboundLocalError: local variable 'num' referenced before assignment". Based on my reading of this guide, I tried replacing line 13 with:
rounded_list_of_floats += ceil(individual_float)
. No dice.What kind of hints or advice could you people provide without giving me the solution entirely?
By the way, I originally got the idea to use the
ceil()
and floor()
function from Real Python's guide called: How to Round Numbers in Python.