2025년 1월 06일 Python (시간,난수,choice,question)

2025. 1. 6. 16:06코딩/python

시간 조사

 

import time

print(time.time())                          # 1970년 1월 1일부터 지난 시간 체크

출력 값

1736140893.4197786  1970년 1월 1일부터 지난 시간  초단위 표기

 

 

import time

t = time.time()
print(time.ctime(t))                       # 현재 시간 표기 걍 외우자

출력값 

Mon Jan  6 14:24:47 2025   당시 시간

 

import time

t = time.time()
print(time.localtime(t))                       # 현재 시간 표기 걍 외우자

출력 값   요일부터 시간 하나하나다 출력해주는듯 

time.struct_time(tm_year=2025, tm_mon=1, tm_mday=6, tm_hour=14, tm_min=27, tm_sec=56, tm_wday=0, tm_yday=6, tm_isdst=0)

 

import time

print("안녕하세요")
time.sleep(1)                                              # 1초 있다가 출력
print("밤에 성시경이 두 명 있으면 뭘까요?")
time.sleep(5)                                              # 5초 있다가 출력
print("야간투시경입니다.")

출력 값

안녕하세요
밤에 성시경이 두 명 있으면 뭘까요?          <----1초 후 출력
야간투시경입니다.                                     <-----5초 후 출력

 

import random

for i in range(5):                                                  # 1~10에서 5개만 출력
    print(random.randint(1,10))                            # random으로 randint 1에서 10사이 숫자 출력ㅊ

출력 값

1~10 사이 숫자 5개 

import random

for i in range(5):                                         # 1~10에서 5개만 출력
    print(random.uniform(1,100))                            # uniform 사용시에는 소숫점 실수 생성

 

출력값  (실수난수 랜덤 생성)

67.65274276531673
44.189808060210794
39.2099600772684
50.203718616214175
61.762456731917965

 

 

choice함수

import random
food = ["짜장만","짬뽕","탕수육","군만두"]
print(random.choice(food))                     #랜덤으로 4개의 항목중에 하나 초이스함

 

출력값

4개 중 1개씩 랜덤 출력

 

shuffle함수

import random
food = ["짜장만","짬뽕","탕수육","군만두"]
print(food)                                                         #["짜장만","짬뽕","탕수육","군만두"] 출력
random.shuffle(food)                              
print(food)                                                         #shufflefh 항목 섞은것 출력

출력값

['짜장만', '짬뽕', '탕수육', '군만두']
['군만두', '짬뽕', '탕수육', '짜장만']  shufflefh 항목 섞은것 출력

 

sort함수(정렬)

import random
nums = random.sample(range(1,46),6)                                 # 1~46 숫자   6개                                
nums.sort()                                                                            # sort        정렬함수
print(nums)                                                        

출력값

1~46 숫자 6개 랜덤 오름차수 정렬

 

 

question 함수

import random

a = random.randint(1,9)
b = random.randint(1,9)
question = "%d + %d = ?" % (a,b)                              
c = int(input(question))          

if c == a + b:                                                   #c값으로 a+b맞췄을시
    print("정답입니다.")
else:                                                                 #else로 틀렸을시
    print("틀렸습니다.")

출력값

1~9사이 a+b퀴즈내고

c값으로 a+b값을 정확하게 입력했을시 if문으로 "정답입니다"출력

틀렸을시 else문으로 틀렸습니다. 출력 

 

 

question2 함수

 

import random

correct = 0
while True:
    a = random.randint(1,9)
    b = random.randint(1,9)
    question = "%d+%d=?(끝낼 때는 0)" % (a,b) # question함수로 a+b=c값 출력
    c=int(input(question))

    if c == 0:
        break                             # if가 correct를 멈출때 break로 멈춤
    elif c == a + b:
        print("정답입니다.")              
        correct = correct + 1             # 무한 루프   c+1로 무한루프 가동하는 듯    
    else:
         print("틀렸습니다.")            
print("%d 개 맞췄습니다." % correct)       #무한 루프 끝났을때 출력함

 

출력값

a+b=c값(1~9사이값 랜덤출력) 무한 출력

0입력시 멈추고 최종 프린트 갯수 출력해줌 

 

 

question3 함수(여유될때 ㄱㄱ)