티스토리 뷰
1. 가위바위보 게임
( 내 풀이 )
1. 유저 : input 으로 '가위', '바위', '보' 중 하나를 입력 -> if문으로 가위는 0, 바위는 1, 보는 2로 변환
2. 컴퓨터 : random 모듈로 0~1 사이의 랜덤 실수를 생성 -> 3을 곱한 후 int() 적용 -> 0, 1, 2 중 하나가 나옴
import random # 파이썬을 설치하면 random.py가 생김, 이 모듈을 메모리에 올리겠다
random.random() # 0~1 사이의 실수
while True:
start = input('가위, 바위, 보 중 하나를 입력하세요 : ')
me = 0
computer = int(random.random() * 3)
if start == '가위':
me = 0
elif start=='바위':
me = 1
elif start=='보':
me = 2
else:
print('다시 입력해주세요')
continue
3. - 비기는 경우 : 숫자가 같을 때
- 이기는 경우 : 가위(0) - 보(2) = -2
바위(1) - 가위(0) = 1,
보(2) - 바위(1) = 1
- 지는 경우 : 가위(0) - 바위(1) = -1
바위(1) - 보(2) = -1
보(2) - 가위(0) = 2
4. 따라서 (유저 - 컴퓨터) 의 결과가 (-2 or 1)일 때 이기고, (-1 or 2)일 때 지는 것으로 if문으로 작성
if me == computer :
print('비겼습니다.')
elif (me - computer == -2) or (me - computer == 1):
print('이겼습니다.') # 가위(0)-보(2), 바위(1)-가위(0), 보(2)-바위(1)
elif (me - computer == -1) or (me - computer == 2):
print('졌습니다.') # 가위(0)-바위(1), 바위(1)-보(2), 보(2)-가위(0)
( 최종 )
import random # 파이썬을 설치하면 random.py가 생김, 이 모듈을 메모리에 올리겠다
random.random() # 0~1 사이의 실수
while True:
start = input('가위, 바위, 보 중 하나를 입력하세요 : ')
me = 0
computer = int(random.random() * 3)
if start == '가위':
me = 0
elif start=='바위':
me = 1
elif start=='보':
me = 2
else:
print('다시 입력해주세요')
continue
if me == computer :
print('비겼습니다.')
elif (me - computer == -1) or (me - computer == 2):
print('졌습니다.') # 가위(0)-바위(1), 바위(1)-보(2), 보(2)-가위(0)
elif (me - computer == -2) or (me - computer == 1):
print('이겼습니다.') # 가위(0)-보(2), 바위(1)-가위(0), 보(2)-바위(1)
print(f'컴퓨터가 낸 것 (0:가위, 1:바위, 2:보): {computer}')
( 다른 풀이 )
import random
rsp = ['가위', '바위', '보']
while True:
computer = random.choice(rsp)
# rand_num = random.randint(0, 2)
# computer = rsp[rand_num]
user = input('가위, 바위, 보 중 하나를 선택 : ')
if user not in rsp:
print('다시 입력해주세요.')
continue
print(f'컴퓨터 : {computer}')
if user == computer:
print('비겼습니다')
elif (user == '가위' and computer == '보') or \
(user == '바위' and computer == '가위') or \
(user == '보' and computer == '주먹'):
print('이겼습니다')
else:
print('졌습니다')
restart = input('계속 하시겠습니까? (Y/N) : ')
if restart.upper() == 'Y':
continue
else:
break
2. 로또 번호 생성
- 1~45까지의 임의의 수 6개
- 중복없음
- 오름차순
( 내 풀이 )
import random
lotto = set() # 중복없게
while len(lotto) < 6: # 개수가 6개가 되는 순간 끝
lotto.add(int(random.random() * 45 + 1)) # 0~44 랜덤생성 후 + 1
print(sorted(lotto)) # 오름차순 정렬
( 다른 풀이 )
import random
lotto = sorted(random.sample(range(1, 46), 6)) # 1~45 중 6개 랜덤 추출 후 정렬
print(f'로또번호: {lotto}')
'Python' 카테고리의 다른 글
16. 파이썬의 상속 (0) | 2024.03.19 |
---|---|
15. 파이썬의 클로저와 데코레이터 (0) | 2024.03.19 |
13. 파이썬의 객체지향과 클래스 (0) | 2024.03.18 |
12. 콜백함수와 람다함수 (0) | 2024.03.18 |
11. 파이썬의 변수 범위 (0) | 2024.03.18 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 솔로의식탁
- EPL정보프로그램
- 박스사이징
- 출력
- CSS
- FOR
- 폼
- DB프로그램만들기
- Enclosing
- Python
- 로또번호생성
- 절대위치
- MySQLdb
- 셋
- 줄 간격
- DB단어장
- HTML
- 닷홈
- 고정위치
- 상대위치
- __call__
- MySQL
- 클래스문
- 파이썬SQL연동
- html이론
- JavaScript
- 변수
- trasform
- 리스트
- animation적용
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함