일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- NameSpace #NS
- lenova #레노버 #노트북
- CI #CD #CI/CD
- OCP
- Python #pakage
- Grid #CSS
- 네트워크 #NW
- bootstrap #css #CSS
- Linux #wc
- 티스토리챌린지
- publishnotreadyaddress
- 백준 #10430
- istio #k8s #kubernetes
- lvm #lv #vg #pv
- jgrp000032 #ocp #
- jmap #jstack
- shell #shell script
- test #비교
- Swap Memory
- Kafka #카프카
- function #사용자 정의 함수
- DB #mariaDB #SQL
- 오블완
- EFK
- PODS #POD #pods #pod #파드 #재기동 #롤링재기동 #rolling
- Excel #엑셀
- EKS
- dump #jattach
- Node #POD #Container
- dify
Archives
- Today
- Total
BEOM_IT
숫자야구 본문
728x90
반응형
# 숫자야구 만들기
# 3개의 숫자를 랜덤하게 만든다.
# 자리와 값이 일치하면 strike
# 값이 존재하면 ball
# 숫자는 중복하지 않고 자리와 숫자를 모두 맞추면 strike out
from random import randint
# 랜덤 숫자 만들기
def base_num():
num=[] # ball count 담을 배열
i=0
new_num=0
while i<3:
new_num=randint(0,9)
if new_num not in num:
num.append(new_num)
i+=1
return num
# 스코어 확인
def score(ball,res):
S_cnt=0 # strike
B_cnt=0 # ball
i=0 # 숫자
while i<len(ball):
if ball[i]==res[i]:
S_cnt+=1
i+=1
elif ball[i] in res:
B_cnt+=1
i+=1
else:
i+=1
return S_cnt,B_cnt
# 실제 플레이
def playBall():
print("숫자 3개 입력하세요!")
i=0 # 입력 숫자
play=[]
while i < 3:
num=int(input("{}번째 숫자 입력:".format(i+1)))
if num > 9:
print("범위는 0~9 입니다.")
continue
if num in play:
print("중복되는 숫자입니다.")
else:
play.append(num)
i+=1
return play
# 시작
QUE=base_num() # 정답
# 정답 확인
# print(QUE)
challenge=0 # 도전 횟수
while 1: # python3에서는 while True 도 성능이 같음
PLAY=playBall() # 플레이
S_cnt,B_cnt=score(QUE,PLAY)
print("{} S {} B".format(S_cnt,B_cnt))
# 정답시 멈춤
if S_cnt==3:
challenge+=1
break
else:
challenge+=1
print("{}번 만에 맞추셨습니다.".format(challenge))
728x90
반응형
'Language > Python' 카테고리의 다른 글
계산기 만들기 (0) | 2024.04.09 |
---|---|
서울시 미세먼지 데이터 가져오기 (0) | 2024.04.09 |
웹 크롤링 - 네이버 증권정보 빼오기 (0) | 2024.04.09 |
로또 번호 추첨기 [Python] (0) | 2023.09.15 |
숫자야구 [Python] (0) | 2023.09.15 |