발전을 위한 기록

[Python] 파이썬으로 시간, 분, 초 변환하기 본문

프로그래밍/파이썬

[Python] 파이썬으로 시간, 분, 초 변환하기

릴릴2 2024. 4. 2. 00:19

파이썬을 사용하여 시간, 분, 초를 변환하는 간단한 코드 입니다!

 

1. 시간 >> 초

def time_to_seconds(hours):
  
    total_seconds = hours * 3600
    return total_seconds

# 사용자로부터 시간을 입력받음
hours = int(input("시간을 입력하세요: "))

# 시간을 초로 변환하여 출력
total_seconds = time_to_seconds(hours)
print("입력한 시간은 총 {} 초입니다.".format(total_seconds))

 

실행 결과

시간을 입력하세요: 2
입력한 시간은 총 7200 초입니다.

2. 시간, 분 >> 초

def time_to_seconds(hours, minutes):
 
    total_seconds = hours * 3600 + minutes * 60
    return total_seconds

# 사용자로부터 시간과 분을 입력받음
hours = int(input("시간을 입력하세요: "))
minutes = int(input("분을 입력하세요: "))

# 시간과 분을 초로 변환하여 출력
total_seconds = time_to_seconds(hours, minutes)
print("입력한 시간과 분은 총 {} 초입니다.".format(total_seconds))

 

실행 결과

시간을 입력하세요: 1
분을 입력하세요: 30
입력한 시간과 분은 총 5400 초입니다.

 


3. 시간, 분, 초 >> 초

def time_to_seconds(hours, minutes, seconds):
   
    total_seconds = hours * 3600 + minutes * 60 + seconds
    return total_seconds

# 사용자로부터 시간, 분, 초를 입력받음
hours = int(input("시간을 입력하세요: "))
minutes = int(input("분을 입력하세요: "))
seconds = int(input("초를 입력하세요: "))

# 시간, 분, 초를 초로 변환하여 출력
total_seconds = time_to_seconds(hours, minutes, seconds)
print("입력한 시간, 분, 초는 총 {} 초입니다.".format(total_seconds))

 

실행 결과

시간을 입력하세요: 1
분을 입력하세요: 30
입력한 시간과 분은 총 5400 초입니다.

4. 초 >> 시간, 분

def seconds_to_time(total_seconds):

    hours = total_seconds // 3600
    remaining_seconds = total_seconds % 3600
    minutes = remaining_seconds // 60
    return hours, minutes

# 사용자로부터 초를 입력받음
total_seconds = int(input("초를 입력하세요: "))

# 초를 시간, 분으로 변환하여 출력
hours, minutes = seconds_to_time(total_seconds)
print("입력한 초는 {} 시간 {} 분입니다.".format(hours, minutes))

 

실행 결과

초를 입력하세요: 7200
입력한 초는 2 시간 0 분입니다.

 


파이썬을 이용한 다른 예제

 

https://riwltnchgo.tistory.com/110https://riwltnchgo.tistory.com/110

 

[Python] 파이썬으로 txt 파일 단어 수 세기

파이썬을 이용하여 텍스트 파일의 단어 빈도수를 분석할 수 있습니다. 주석 없는 코드 def count_word_frequency(text): words = text.split() frequency = {} for word in words: word = word.lower().strip(",.!?") frequency[word] = fr

riwltnchgo.tistory.com

 

https://riwltnchgo.tistory.com/95

 

[Python] 파이썬으로 센티미터 인치로 변환하기

사용자로부터 센티미터 값을 입력받아 인치로 변환하는 코드 입니다. 설명은 주석을 참고해주세요! # 센티미터를 인치로 변환하는 함수 def cm_to_inch_converter(cm): inches = cm / 2.54 return inches # input을

riwltnchgo.tistory.com

 

https://riwltnchgo.tistory.com/87

 

[Python] 파이썬을 이용한 섭씨, 화씨 변환

사용자로부터 섭씨 온도를 입력받아 화씨 온도로 변환하는 코드 입니다! 설명을 주석을 참고해주세요! # 섭씨를 화씨로 변환하는 함수 선언 def convert(celsius): # 화씨 = (섭씨 * 9/5) + 32 return (celsius *

riwltnchgo.tistory.com

 

https://riwltnchgo.tistory.com/73

 

[Python] 파이썬을 이용한 구구단 출력하기

파이썬을 이용한 구구단 출력하기 구구단 출력하기 num = int(input("구구단을 출력할 숫자를 입력하세요 (1에서 9 사이): ")) if 1 5,6,7,8,9,10,11 결과

riwltnchgo.tistory.com

 

728x90