비전공개미 개발노트
python 4일차 - 가위바위보, UP&DOWN 게임만들기 본문
반응형
SMALL
과제¶
In [ ]:
## 내 풀이
In [ ]:
'''
가위(0), 바위(1), 보(2) 게임
com : random
user : 입력
<한판만>
판정 :
당신은 ?를 냈습니다
com은 ?를 냈습니다
당신이 이겼습니다
당신이 졌습니다
비겼습니다
'''
In [132]:
import random
# 입력
user = input('가위, 바위, 보 하나를 입력해주세요 > ')
com = random.randint(0, 2)
randcom = ''
if com == 0:
randcom = '가위'
elif com == 1:
randcom = '바위'
elif com == 2:
randcom = '보'
# 비겼을경우
if randcom == user:
output = '비겼습니다'
# 이겼을경우
elif randcom == '가위' and user == '바위':
output = '당신이 이겼습니다'
elif randcom == '바위' and user == '보':
output = '당신이 이겼습니다'
elif randcom == '보' and user == '가위':
output = '당신이 이겼습니다'
# 졌을경우
elif randcom == '가위' and user == '보':
output = '당신이 졌습니다'
elif randcom == '바위' and user == '가위':
output = '당신이 졌습니다'
elif randcom == '보' and user == '바위':
output = '당신이 졌습니다'
# 판정
print('당신은', user, '를 냈습니다')
print('com은', randcom, '를 냈습니다')
print(output)
# 결과 출력
가위, 바위, 보 하나를 입력해주세요 > 보
당신은 보 를 냈습니다
com은 보 를 냈습니다
비겼습니다
In [ ]:
'''
숫자 찾기 게임
1 ~ 100 -> random 한개의 숫자 추출 > ex)67
user 입력 > 50 --> 10번의 기회
너무 작습니다
user 입력 > 75
너무 큽니다
user 입력 > 67
정답입니다
맞췄을 경우 : game clear
못 맞췄을 경우 : game over
'''
In [153]:
## up & down 숫자맞추기 게임
randnum = random.randrange(1, 100)
# randnum
# rangenum = 10
for i in range(10): # 10번의 기회를 준다
rot = int(input('숫자를 입력해주세요 > '))
if i < 9: # i가 10번째보다 적으면 돌리고
if randnum > rot:
out = 'UP'
print(out)
elif randnum < rot:
out = 'DOWN'
print(out)
elif randnum == rot:
out = 'GAME CLEAR!'
print(out)
break
else: # i가 10번째가 넘으면 종료한다
print('GAME OVER~')
숫자를 입력해주세요50
DOWN
숫자를 입력해주세요60
DOWN
숫자를 입력해주세요40
DOWN
숫자를 입력해주세요30
UP
숫자를 입력해주세요21
UP
숫자를 입력해주세요22
UP
숫자를 입력해주세요23
UP
숫자를 입력해주세요24
UP
숫자를 입력해주세요25
UP
숫자를 입력해주세요26
GAME OVER~
풀이¶
In [63]:
## 선생님 풀이
In [ ]:
'''
가위(0), 바위(1), 보(2) 게임
com : random
user : 입력
<한판만>
판정 :
당신은 ?를 냈습니다
com은 ?를 냈습니다
당신이 이겼습니다
당신이 졌습니다
비겼습니다
'''
In [131]:
import random as r
# 랜덤
com_num = r.randrange(3) # 0 ~ 2
print(com_num)
# 입력
user_num = int(input('가위(0) 바위(1) 보(2) > '))
result = 0
# 판정
''''''
# win 0-2 1-0 2-1
if user_num == 0 and com_num == 2: result = 0
elif user_num == 1 and com_num == 0: result = 0
if user_num == 2 and com_num == 1: result = 0
# lose 2-0 0-1 1-2
if user_num == 2 and com_num == 0: result = 1
elif user_num == 0 and com_num == 1: result = 1
if user_num == 1 and com_num == 2: result = 1
# drew 0-0 1-1 2-2
# if user_num == com_num: result = 2
if user_num == 2 and com_num == 2: result = 2
elif user_num == 0 and com_num == 0: result = 2
if user_num == 1 and com_num == 1: result = 2
''''''
'''
# win 0-2 1-0 2-1
com user
2 - 0 + 2 = 4 % 3 -> 1
0 - 1 + 2 = 1 % 3 -> 1
1 - 2 + 2 = 1 % 3 -> 1
# lose 2-0 0-1 1-2
com user
0 - 2 + 2 = 0 % 3 -> 0
1 - 0 + 2 = 3 % 3 -> 0
2 - 1 + 2 = 3 % 3 -> 0
# drew 0-0 1-1 2-2
com user
0 - 0 + 2 = 2 % 3 -> 2
1 - 1 + 2 = 2 % 3 -> 2
2 - 2 + 2 = 2 % 3 -> 2
'''
# 함수생성
def message(num):
msg = ''
if num == 0 : msg = '가위'
elif num == 1 : msg = '바위'
else: msg = '보'
return msg
# 공식 계산법
result = (com_num - user_num + 2) % 3
msg = ''
if result == 1:
msg = '당신이 이겼습니다'
elif result == 0:
msg = '당신이 졌습니다'
else:
msg = '비겼습니다'
# 결과 출력
'''
user = ''
if user_num == 0 : user = '가위'
elif user_num == 1 : user = '바위'
else: user = '보'
com = ''
if com_num == 0 : com = '가위'
elif com_num == 1 : com = '바위'
else: com = '보'
'''
user = message(user_num)
com = message(com_num)
print('당신 :', user, '/ 컴퓨터 :', com)
print(msg)
2
가위(0) 바위(1) 보(2) > 1
당신 : 바위 / 컴퓨터 : 보
당신이 졌습니다
In [ ]:
'''
숫자 찾기 게임
1 ~ 100 -> random 한개의 숫자 추출 > ex)67
user 입력 > 50 --> 10번의 기회
너무 작습니다
user 입력 > 75
너무 큽니다
user 입력 > 67
정답입니다
맞췄을 경우 : game clear
못 맞췄을 경우 : game over
'''
In [206]:
# random number
rand_num = r.randint(1, 100)
print(rand_num)
w = 0
while w < 10:
# user number input
user_num = int(input('숫자 입력 > '))
# finding
clear = False
msg = 0
if user_num > rand_num:
msg = 0
elif user_num < rand_num:
msg = 1
else:
clear = True
break # 맞췄을때 break로 빠져나간다
# message 출력
if msg == 0:
print('너무 큽니다')
elif msg == 1:
print('너무 작습니다')
w += 1
# 결과 출력
if clear == True:
print('Game Clear!!!')
else:
print('Game Over~')
32
숫자 입력 > 50
너무 큽니다
숫자 입력 > 40
너무 큽니다
숫자 입력 > 30
너무 작습니다
숫자 입력 > 31
너무 작습니다
숫자 입력 > 32
Game Clear!!!
work6.ipynb
0.01MB
반응형
LIST
'프로그래밍 > Python' 카테고리의 다른 글
python 5일차 - OOP, 클래스 (0) | 2022.09.02 |
---|---|
python 4일차 - class(클래스) (0) | 2022.09.01 |
python 4일차 - math, random (0) | 2022.09.01 |
python 3일차 - file write/read, try catch 예외처리 (0) | 2022.08.31 |
python 3일차 - 함수, 매개변수, 가변인자 (0) | 2022.08.31 |
Comments