Covert seconds to hours and get input and use function in Python - python

Code:1- It is working
def seconds_to_hours(seconds):
hours = seconds / (60 * 60)
return hours
print(seconds_to_hours(5000))
Code:2 - It's not working
seconds = input("Enter how many seconds convert to hours?: ")
def seconds_to_hours(seconds):
hours = seconds / (60 * 60)
return hours
print(seconds_to_hours)
Error is: <function seconds_to_hours at 0x037FC2B0>
How can I solve this?

Don't use twice the same name for the main param seconds that gets the input, and the function param, also convert the input to int
def seconds_to_hours(secs):
return secs / (60 * 60)
seconds = int(input("Enter how many seconds convert to hours?: "))
print(seconds_to_hours(seconds))

Related

Evaluating time with python [duplicate]

This question already has answers here:
How do I convert seconds to hours, minutes and seconds?
(18 answers)
Closed 10 months ago.
I need to write a program that reads in seconds as input, and outputs the time in hours, minutes, and seconds using python.
seconds = int(input())
minutes = seconds // 60
hours = minutes // 3600
seconds_left = + (seconds - hours)
print(f'Hours: {hours}')
print(f'Minutes: {minutes}')
print(f'Seconds: {seconds_left}')
This is what I'm currently running and it's not getting the desired output. Question in mind uses 4000 as an input and outputs 1 hour, 6 min, and 40 seconds
When you divide to get (e.g.) the hours, you should also take the mod in order to just carry forward the remainder:
>>> seconds = 4000
>>> hours = seconds // 3600
>>> seconds = seconds % 3600
>>> minutes = seconds // 60
>>> seconds = seconds % 60
>>> hours, minutes, seconds
(1, 6, 40)
This is equivalent to multiplying the int quotient by the divisor and subtracting:
>>> seconds = 4000
>>> hours = seconds // 3600
>>> seconds -= hours * 3600
>>> minutes = seconds // 60
>>> seconds -= minutes * 60
>>> hours, minutes, seconds
(1, 6, 40)

How to convert a hh:mm:ss to seconds without a time_string in python?

For my assignment the requirements are to state the function like this:
def to_seconds(hours, minutes, seconds):
Most of the programs that I have seen run it with time_str between the parentheses. I have been attempting some different possibilities but I cannot seem to understand why my list doesn't want to convert to an integer so I can count all the individual variables.
Here is my code:
def to_seconds(hours, minutes, seconds):
hh, mm, ss = str((hours * 3600, minutes * 60, seconds,)).split()
return int(hh) + int(mm) + int(ss)
You added unnecessary complication:
just do:
def to_seconds(hours, minutes, seconds):
return hours*3600 + minutes*60 + seconds
Your current error is:
hh = '(0,'
mm = '60,'
ss = '0)'
As you know we can't convert '(0,','60,' or '0)' to int.
I am sorry, I figured it out with help of the comments. This seems to be working for me:
def to_seconds(hours, minutes, seconds):
hh, mm, ss = (hours * 3600, minutes * 60, seconds,)
return int(hh) + int(mm) + int(ss)

Python Military Time formula and string formatting

I have created a function to take a string input "minutes-since-midnight" and convert it into military time.For example- 365 = 0605,441 = 0721,864 = 1424. For some reason my outputs are: 066, 077, 1414. Can someone please explain to me what I am doing wrong
def military_time(minutes):
if minutes < 600:
hour = minutes // 60
minute = minutes % 60
print("0{0:0}{0:0}".format(hour,minute))
elif 600 < minutes < 720:
hour = minutes // 60
minute = minutes % 60
print("{0:0}{0:0}".format(hour,minute))
elif minutes == 720:
hour = 1200
print(hour)
else:
hour = ((minutes-720) // 60) + 12
minute = (minutes) % 60
print("{0:0}{0:0}".format(hour,minute))
you can use format to pad your integers with zeros if necessary:
def military_time(minutes):
minutes %= 1440 # does not make sense if more than 24h
h, m = divmod(minutes, 60)
print("{0:02d}{1:02d}".format(h, m))
military_time(365) # 0605
military_time(441) # 0721
military_time(864) # 1424
also note that your format string referenced the first argument twice (you had {0}{0} instead of {0}{1}).

Convert time object to minutes in Python 2

I want to convert a time.time() object to minutes.
In my program, I did this:
import time
start = time.time()
process starts
end = time.time()
print end - start
Now I have the value 22997.9909999. How do I convert this into minutes?
You've calculated the number of seconds that have elapsed between start and end. This is a floating-point value:
seconds = end - start
You can print the number of minutes as a floating-point value:
print seconds / 60
Or the whole number of minutes, discarding the fractional part:
print int(seconds / 60)
Or the whole number of minutes and the whole number of seconds:
print '%d:%2d' % (int(seconds / 60), seconds % 60)
Or the whole number of minutes and the fractional number of seconds:
minutes = int(seconds / 60)
print '%d m %f s' % (minutes, seconds - 60 * minutes)

I am creating a running time calculator and am having trouble using functions

Here is my current code:
def get_input():
pace = str(input("Enter pace [mm:ss]: "))
distance = float(input("Enter distance [miles]: "))
mm, ss = int(pace.split(":")[0]), int(pace.split(":")[1])
return(pace, distance, mm, ss)
def calculate_time(pace, distance, mm, ss):
print(pace)
print(mm)
print(ss)
new_sec = mm * 60
full_sec = ss + new_sec
print(full_sec)
print(distance)
total_time_sec = full_sec * distance
print(total_time_sec)
seconds_per_mile = total_time_sec / 60
hours = int(seconds_per_mile // 60)
print(seconds_per_mile)
print(hours)
minutes = int((total_time_sec - (hours * 3600))//60)
print(minutes)
seconds = int(total_time_sec - ((hours * 3600) + (minutes * 60)))
print(seconds)
print(pace, full_sec, distance)
return(seconds, minutes, hours)
def display_time(pace, distance, mm, ss, seconds, minutes, hours):
if seconds < 10:
print(hours,":",minutes,":0",seconds, sep="")
else:
print(hours,":",minutes,":",seconds, sep="")
return()
def main():
pace, distance, mm, ss = get_input()
new_sec, full_sec, total_time_sec, seconds_per_mile, hours, minutes, seconds = calculate_time(pace, distance, mm, ss)
display_time(pace, distance, mm, ss, seconds, minutes, hours)
main()
I'm not terribly good at using functions. I don't believe I have a full understanding of returning stuff any whatnot, either.
This is the error I'm getting:
Traceback (most recent call last):
File "C:/Python33/homework 3 test.py", line 36, in <module>
main()
File "C:/Python33/homework 3 test.py", line 34, in main
new_sec, full_sec, total_time_sec, seconds_per_mile, hours, minutes, seconds = calculate_time(pace, distance, mm, ss)
ValueError: need more than 3 values to unpack
The point of the function is to get the user to input their pace and distance and output the time needed to run the distance.
For example:
Enter pace [mm:ss]: 8:15
Enter distance [miles]: 26.2
3:36:09
If anyone could explain what I'm doing wrong, I would greatly appreciate it. I don't necessarily want to know how to do it to the tee, but I am having a lot of trouble and need a bit of a boost.
Your code is a bit hard to read (check out PEP-8 (Style Guide for Python code)), but one obvious problem with your function calculate_time() is that it does
return(seconds, minutes, hours) # return a three-element tuple
but that main() tries to unpack that into six variables:
new_sec, full_sec, total_time_sec, seconds_per_mile, hours, minutes, seconds = calculate_time(pace, distance, mm, ss)
Some comments:
pace = str(input("Enter pace [mm:ss]: "))
mm, ss = int(pace.split(":")[0]), int(pace.split(":")[1])
is overly complex. input() already returns strings, and there's a nifty Python feature called a list comprehension that you could use here:
pace = input("Enter pace [mm:ss]: ")
mm, ss = [int(item) for item in pace.split(":")]

Categories

Resources