Reputation: 23
as you can probably tell, I am a python beginner. In this program, I am trying to use a function but instead of determining a value for minutes, I want the user to input a value. For the return command of my function, I first used a comma to separate the variables from the string, however, this literally printed the commas in the result. So instead, I used a + operation but had to turn my erstwhile integer values into strings. Is there any way I could have printed the values without turning them to strings while also avoiding the type error?
here's my code
def minutes_to_hours (minutes):
hours = minutes/60
seconds= minutes*60
return str(minutes) + " minutes is equivalent to " + str(hours) + " hours" + ". This is also equivalent to " + str (seconds) + " seconds."
m= int((input ("please enter the value of minutes you want to convert ")))
print(minutes_to_hours(m))
Upvotes: 2
Views: 94
Reputation: 3968
One of the approach out many other approaches will be to let your function return a list and you print the list by specifying its index.
def minutes_to_hours (minutes):
hours = minutes/60
seconds= minutes*60
return [minutes, hours, seconds] ## return your calculated value as list
m= int((input ("please enter the value of minutes you want to convert ")))
result = minutes_to_hours(m)
print(f"{result[0]} minutes is equivalent to {result[1]} hours. This is also equivalent to {result[2]} seconds.")
Hope this helps :)
Upvotes: 0
Reputation: 351
Yes you can use comma between variables and strings but it requires changes
def func(m):
s=m*60
h=m/60
return s,h
m= int((input ("please enter the value of minutes you want to convert ")))
s,h=func(m)
print(m,"minutes are equivalent to",s,"seconds and",h,"hours)
Upvotes: 0
Reputation: 1223
You can also do this:
def minutes_to_hours (minutes):
hours = minutes/60
seconds= minutes*60
return "{} minutes is equivalent to {} hours. This is also equivalent to {} seconds.".format(minutes, hours,seconds)
m= int((input ("please enter the value of minutes you want to convert ")))
print(minutes_to_hours(m))
Upvotes: 1
Reputation:
No, to print integers you need to convert them to strings before, so your code is correct.
Upvotes: 0