I am completely new to python and programming but I am trying to learn it using more practical approach.
What I am trying to do is an exercise for converting different units, e.g. pounds -> kilograms, foot -> meter etc.
I have defined all the functions for different unit pairs:
unit_list = ['kg_to_g','g_to_kg','inch_to_cm','cm_to_inch',
random_unit = random.choice(unit_list)
lower = 0.1001
upper = 2000.1001
range_width = upper - lower
ranval = round(random.random() * range_width + lower, 2)
if answer == random_unit(ranval):
TypeError: 'str' object is not callable
Because you've enclosed the function names (in the list) in quotes, they have become strings.
Change your list to:
unit_list = [kg_to_g, g_to_kg, inch_to_cm, cm_to_inch, ft_to_cm, cm_to_ft, yard_to_m, m_to_yard, mile_to_km, km_to_mile, oz_to_g, g_to_oz, pound_to_kg, kg_to_pound, stone_to_kg, kg_to_stone, pint_to_l, l_to_pint, quart_to_l, l_to_quart, gal_to_l, l_to_gal, bar_to_l, l_to_bar]
And now it is a list of functions, which can be called like this:
unit_list(34), for example.
random_unit(ranval) should not throw an exception.
Note also that comparing floats (
if answer == random_unit(ranval)) will most likely cause you problems. See Is floating point math broken? for some detailed explanations of why this is.
As you are rounding you may get away with it, but it's good to be aware of this and understand that you need to deal with it in your code.