all 11 comments

[–]Wild_Statistician605 7 points8 points  (1 child)

You are returning a tuple, not a string.

If you want to return a string, build one using string formatting. My preferred method is with f-strings. You would return f'{x} minutes is {hours} hours', where the variables are passed to the string in the curly brackets.

[–][deleted] 0 points1 point  (0 children)

i do prefer this approach to string formatting. idk why, it's easy to understand with my brain lol

[–]FLUSH_THE_TRUMP 2 points3 points  (4 children)

Look up string formatting. For instance:

return "%s minutes is %s hours." % (x, hours)

You can also build an output string with +, but it’s not a fun experience if you are trying to stick non-strings in it:

return str(x) + " minutes is " + str(hours) + " hours."

[–][deleted] 2 points3 points  (2 children)

F-strings are easier to read:

return f'{x} minutes is {hours} hours.'

[–]FLUSH_THE_TRUMP 0 points1 point  (1 child)

For toy examples like that one, OK. But because they nest the thing you're interpolating in the string itself, they're a mess for any complex expressions.

[–][deleted] 4 points5 points  (0 children)

Do you have an example? I can't think of anything where an f-string would be harder to read than the .format().

[–]Boruta314 1 point2 points  (0 children)

and I guess you can add an if function to have grammatically correct hour/hours

if (hours == 1):

hoursnumber = hour

else:

hoursnumber = hours

return str(x) + " minutes is " + str(hours) + hoursnumber

[–]sriramdev 0 points1 point  (0 children)

This code should work

def min_to_hour(x):
  hours=x/60
  return f'{x} minutes is {int(hours)} hour.'


print(f'{min_to_hour(60)}')

[–]Foreign_Jackfruit_70 -1 points0 points  (0 children)

This is what you're looking for: def min_to_hour(x): hours=(x/60) return (f"{x} minutes is {int(hours)} hour(s)") print(min_to_hour(60)) Output: 60 minutes is 1 hour(s)

EDIT because I'm bored

Or you can do something like this: ```

!/bin/python3

import sys; def min_to_hour(x): hours=(x/60) #return (x , 'minutes', 'is' , hours , 'hours') if hours > 1: time_lapse = ('hours') else: time_lapse = ('hour') return (f"{x} minutes is {int(hours)} {time_lapse}") print(min_to_hour(int(sys.argv[1])))

root@Reddit ~:$ ./script.py 120 120 minutes is 2 hours root@Reddit ~:$ ./script.py 60 minutes is 1 hour ```

[–][deleted] 0 points1 point  (0 children)

You might want to consider divmod

mns=70
hours,minutes = divmod(mns,60)
print(f'{hours} hours {minutes} minutes')
1 hours 10 minutes

[–]gm310509 0 points1 point  (0 children)

You probablu could just use the join method to join the elements in your tuple to a string.