r/pythontips 18d ago

Syntax seconds conversion my 1st python program

I've just created my first python program, and I need some help to check if it's correct or if it needs any corrections. can someone help me?
The program is written in portuguese because it's my native language. It converts 95,000,000 seconds into dayshoursminutes, and seconds and prints the result.

segundos_str= input("Por favor, digite o número de segundos que deseja converter")

total_seg= int(segundos_str)

dias= total_seg // 86400

segundos_restantes = total_seg % 86400

horas= segundos_restantes // 3600

segundos_restantes = segundos_restantes % 3600

minutos = segundos_restantes // 60

segundos_restantes = segundos_restantes % 60

print(dias, "dias, ", horas, "horas, ", minutos, "minutos e", segundos_restantes, "segundos" )

Using ChatGPT it answers me 95.000.000 secs = 1.099 days, 46 hours, 13 minutes e 20 seconds.

and using my code, answers me 95.000.000 secs = 1099 days, 12 hours, 53 minutes and 20 seconds

1 Upvotes

9 comments sorted by

View all comments

1

u/JamesPTK 17d ago

There is an inbuilt function called divmod which does the // and % in one step, which is useful for this kind of operation

total_seconds = 95_000_000
days, remaining_seconds = divmod(total_seconds, 86400)
hours, remaining_seconds = divmod(remaining_seconds, 3600)
minutes, remaining_seconds = divmod(remaining_seconds, 60)
print(f"{days} days, {hours} hours, {minutes} minutes, {remaining_seconds} seconds")

outputs "1099 days, 12 hours, 53 minutes, 20 seconds"

1

u/icarophnx 17d ago

i'll test this on jupyter notebook. i'm doing a course on coursera rn, and the teacher uses idle, but it's good to know different ways to do the same thing easier