0

This is just part of the question. It has error that missing 1 required positional argument 'mm' when running it. I know the problem is that it run like time_to_minutes((h,mm),) what can i do to make it run like time_to_minutes(h,mm)?

def time_to_minutes(h,mm):
    time = h*60 + mm
    return time

def extract_time(time):
    h=int(time[:-3])
    mm=int(time[-2:])
    return h,mm

def time_between(a,b):
    first = time_to_minutes(extract_time(a))
    return first

1 Answers1

6

Use the star (*) operator to unpack the tuple:

first = time_to_minutes(*extract_time(a))
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • 1
    aka the "splat" operator :) – Adam Smith Feb 06 '16 at 00:04
  • @AdamSmith I didn't know it's called that, thanks :-) – Reut Sharabani Feb 06 '16 at 00:04
  • 1
    It's a borrowed term from other languages. Python doesn't give it a name. http://stackoverflow.com/questions/2322355/proper-name-for-python-operator – Adam Smith Feb 06 '16 at 00:05
  • 3
    To explain what happens: if you send in a tuple as an argument it will still be one parameter, just as a list would be one or a string with two letters would be one and not two parameters. If you instead unpack the tuple with `*` it will become two separate variables (works on lists as well and dicts for named arguments (then with two `**`)). – olofom Feb 06 '16 at 00:09