Python Forum
Rounding exercise: UnboundLocalError: local variable referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rounding exercise: UnboundLocalError: local variable referenced before assignment
#1
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 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.
Reply


Messages In This Thread
Rounding exercise: UnboundLocalError: local variable referenced before assignment - by Drone4four - Feb-15-2020, 12:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rounding without using Python embedded functions NewCoder 3 2,708 Sep-06-2018, 10:23 PM
Last Post: woooee
  UnboundLocalError: local variable 'a' referenced before assignment fad3r 3 16,543 Jun-20-2018, 05:43 PM
Last Post: nilamo
  why am I getting "local variable 'x' referenced before assignment"? wlsa 6 9,095 Jun-16-2018, 05:31 PM
Last Post: buran
  variable referenced before assignment Niko047 4 22,937 Aug-04-2017, 07:55 PM
Last Post: nilamo
  local variable 'l' referenced before assignment... darkreaper1959 4 7,431 Jan-21-2017, 08:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020