Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function help
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
Can you explain what the function does and why it is called minute()?
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?
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
        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
        return hr

# 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)")
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}")
there are 10800.0 second in 3 hour
buran likes this post
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}')

Forum Jump:

User Panel Messages

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