function help - Printable Version +- Python Forum ( https://python-forum.io)+-- Forum: Python Coding ( https://python-forum.io/forum-7.html)+--- Forum: General Coding Help ( https://python-forum.io/forum-8.html)+--- Thread: function help ( /thread-30801.html) |

function help - Nickd12 - Nov-06-2020
this might be a dumb question but i have this function can i set what i want it to return using a variable. for example if i call my minute_ function i set x = 2 can i have another variable indicating what i want it to return. i want to return either minute = x * second()or min_as_second = m * second() depending on the situation. im just trying to avoid if statements or is that the only way? def minute_(x): m = second() * 60 # = 60 minute = x * second() min_as_second = m * second() return min_as_second RE: function help - Gribouillis - Nov-06-2020
Can you explain what the function does and why it is called minute()? RE: function help - jefsummers - Nov-07-2020
Let's look at your code line by line. First, you define a function, minute_ that takes one parameter, presumably numeric. OK, though better if you use a more descriptive name than x Next you declare a variable, m, as being equal to the return value from another function called second(). The function second() takes no parameters but returns (hopefully) a numeric value. This is then multiplied by 60 before being stored in m. I would point out that typically you would divide the number of seconds by 60 to get the number of minutes, but perhaps that is not what you are doing. minute is then calculated by taking the passed value x and multiplying by another call to the function second(), whatever that returns. min_as_second is then given the value of m (which is 60 * that call to second) multiplied by yet another call to second. Note that if second() returns different values depending on the current time or the like, you may get different results from your 3 calls to the function second(). You then return that value. I suspect you have logic errors, as this is not what you want. Perhaps I am wrong. What does the function second() do, and what does it return? RE: function help - Nickd12 - Nov-08-2020
i do apologize i was messing around with finding different ways to do different things just exploring and learning. i was messing around having fun with time conversion. this is what i came up with im sure there is a better way but i was asking if its possible to avoid all the if statements def conversion(number, time, hms): if time == "minute": if hms == "second": minute_ = number * 60 elif hms == "minute": minute_ = number * 1 else: return return minute_ elif time == "hour": if hms == "second": hr = number * 3600 # 3600 seconds in an hour elif hms == "minute": hr = number * 60 elif hms == "hour": hr = number * 1 else: return return hr else: return # print(hour(1, h="second")) number = 1 # one hour time = "hour" # time in hms = "second" # seconds in a hour conver = conversion(number, time, hms) print(f"there are {conver} {hms}(s) in a {time}(s)") RE: function help - bowlofred - Nov-08-2020
You could do something like this. Should use some formatting, but should have similar output. factor = { "hour": 60**2, "minute": 60, "second": 1 } def conversion(number, source, target): multiplier = factor[source] / factor[target] return number * multiplier number = 3 # count time = "hour" # unit in hms = "second" # unit out ans = conversion(number, time, hms) print(f"there are {ans} {hms} in {number} {time}")
RE: function help - Gribouillis - Nov-08-2020
Extending on bowlofred's code, you could use an extensible dictionary of units expressed in the International System of Units and use it to convert from one unit to an other. _si = { 'hour': 3600, 'minute': 60, 'second': 1, 'millisecond': 1e-3, 'microsecond': 1e-6, } def as_si(unit, value=1): return _si[unit] * value def set_si(unit, value): _si[unit] = value set_si('day', 24 * as_si('hour')) set_si('year', 365.25 * as_si('day')) def conversion(value, from_unit, to_unit): return as_si(from_unit, value)/as_si(to_unit) if __name__ == '__main__': number = 3 u1 = 'hour' u2 = 'minute' ans = conversion(number, u1, u2) print(f'There are {ans} {u2} in {number} {u1}') |