열심히 고민하면서 만들어보고 있는데 아직 개념도 그렇고 적용에도 어려움을 겪고 있어 계속해서 시도하는 중이다.
import random
rn = random.randint(1, 100)
coin = 0
print('Up-Down Game을 시작합니다.')
print('1~100 사이의 숫자를 입력하세요.')
while True :
try :
input_num = int(input('숫자를 입력하세요 : '))
coin += 1
if (0 > input_num or 100 < input_num):
print('1~100 사이의 숫자를 입력하세요')
elif (input_num < rn):
print('UP')
elif (input_num > rn):
print('DOWN')
else:
print(coin, '번째 시도에 맞혔습니다. ')
continue_quit = str(input('한 판 더? (Y/N) '))
if continue_quit == 'Y' :
rn = random.randint(1, 100)
print('Up-Down Game을 시작합니다.')
print('1~100 사이의 숫자를 입력하세요.')
while True :
try :
input_num = int(input('숫자를 입력하세요 : '))
coin += 1
if (0 > input_num or 100 < input_num):
print('1~100 사이의 숫자를 입력하세요')
elif (input_num < rn):
print('UP')
elif (input_num > rn):
print('DOWN')
else:
print(coin, '번째 시도에 맞혔습니다. ')
continue_quit = str(input('한 판 더? (Y/N) '))
except ValueError:
print('정수로 입력해주세요.')
elif continue_quit == 'N' :
break
except ValueError:
print('정수로 입력해주세요.')
import random
def up_down_game():
start_game = str(input('Up & Down 게임을 시작하시겠습니까? (Y/N) '))
while start_game.upper == 'Y' :
try :
print('Up & Down 게임을 시작합니다.')
rn = random.randint(1, 100)
print('1~100 사이의 숫자를 입력하세요.')
input_num = int(input('숫자를 입력하세요 : '))
tries = 0
if (0 > input_num or 100 < input_num):
tries += 1
print('1~100 사이의 숫자를 입력하세요')
elif (input_num < rn):
tries += 1
print('UP')
elif (input_num > rn):
tries += 1
print('DOWN')
else:
tries += 1
print(tries, '번째 시도에 맞혔습니다. ')
if continue_game.upper == 'Y' :
restart()
elif continue_game.upper == 'N':
print('총', tries, '번 도전하셨습니다.')
up_down_game()
else :
continue_game = str(input('한 판 더? (Y/N) '))
except ValueError:
print('정수로 입력해주세요.')
else:
up_down_game()
def restart() :
continue_game = str(input('한 판 더? (Y/N) '))
while continue_game.upper == 'Y' :
rn = random.randint(1, 100)
tries = 0
try :
input_num = int(input('숫자를 입력하세요 : '))
tries += 1
if (0 > input_num or 100 < input_num):
print('1~100 사이의 숫자를 입력하세요')
elif (input_num < rn):
print('UP')
elif (input_num > rn):
print('DOWN')
else:
print(tries, '번째 시도에 맞혔습니다. ')
continue_game = str(input('한 판 더? (Y/N) '))
if continue_game == 'y' or continue_game == 'Y' :
restart()
else :
print('총', tries, '번 도전하셨습니다.')
break
except ValueError:
print('정수로 입력해주세요.')
else up_down_game()
up_down_game()
아직 미완성이지만 구현까지 해보고 while문이 아닌 for문으로 만들어볼 생각이다.
오늘 겪은 문제 중에 함수를 시작하면 '시작하시겠습니까? (Y/N)'에서 어떤 문자를 입력해도 게임이 시작된다는 점이었다.
이는, while문의 시작 조건에 while start_game == 'y' or 'Y' 부분 때문이었는데 컴퓨터가 똑똑한건지 멍청한건지 조건을 보다 명확히 해주지 않아서 Y를 치던 N을 치던 ~를 치던 모두 True로 보고 while문을 실행했던 것이다.
.upper나 .lower로 입력값에 대문자를 치던 소문자를 치던 통일해서 조건문에 넣을 수 있게 (while start_game.upper == 'Y' :) 만들어주니 이 문제는 해결되었다.
아직 완성까지는 멀었지만 이것 저것 해보는게 재미있다.
여러 방향으로 시도해보면서 문제를 풀면 더 재밌어 지는 것 같아요~!