
1.컴퓨터는 임의의 숫자(A)를 제시한다.
2.사용자는 A를 맞추기 위한 숫자(B)를 제시한다.
3.B가 틀릴 시 A와 비교하여 힌트를 제시한다.
4.사용자가 A를 맞출 때까지 (2) 와 (3)을 반복한다.
import random
임의의 숫자를 생성해줄 random모듈을 추가해준다.
random_num = random.randint(1, 100)#1 ~ 100사이 정수중 하나를 정한다. restart = 'y' # 재시작여부 y면 재시작 , n은 while문을 나와 게임 종료 predic_num = 0 # 사용자가 예측한 숫자 restart_count = 0 #재시작된 적이 있는지 확인하는 변수 maximum_count = 0 #제일 많이 시도한 횟수 count = 0 # 현재 게임에서 시도한 횟수
게임에 필요한 변수들을 선언 및 초기화해준다.
while random_num != predic_num and restart == 'y': if maximum_count != 0 and restart_count == 0: print(f'이전 게임 플레이어 최고 시도 점수 : {maximum_count}') restart_count = 1
정답이 아니거나 재시작 여부가 y인 경우 while문을 반복하며,
게임을 한 번 이상 진행한 경우(maximum_count이 0이 아니고 ,restart_count 가 0일때)
플레이어의 이전 최대 시도횟수를 출력해준다.
# enter prediction number predic_num = input('1 ~ 100 사이 숫자를 맞춰보세요 : ') try: # user entered integer, and consverts type str to int predic_num = int(predic_num) except: # user entered something other print('자연수를 입력해주세요 (1 ~ 100)') print('') continue
이제 게임을 시작해보자
숫자를 입력받았지만, 이 숫자의 자료형은 문자열로, 숫자처럼 비교가 불가능하다.
그러므로, 정수형으로 변환해준다. 오류가 생긴다면 숫자를 입력한게 아니므로 continue를 이용해 반복문의 처음으로 돌아와 다시 숫자를 입력받는다.
if 1 <= predic_num <= 100: # user enter 1 ~ 100 integer if random_num < predic_num: # predic_num bigger than random number print('다운') count += 1 elif random_num > predic_num: # predic_num smaller than random number print('업') count += 1 else: # prediction is correct count += 1 print(f'맞았습니다.\n 시도한 횟수 : {count}') if maximum_count < count: maximum_count = count count = 0 # # check regame (y or n) restart = input('다시 하시겠습니까? (y/n):') while restart != 'y' and restart != 'n': # user entered something other restart = input('다시 하시겠습니까? (y/n):') # change random_number and restart_count random_num = random.randint(1, 100) restart_count = 0 else: # user enter bigger than 100 or smaller than 1 print('1 ~ 100사이 자연수를 입력해주세요.') print('')
사용자가 예측한 숫자(B)가 1 ~ 100을 벗어난 숫자를 입력 시, 1 ~ 100사이 숫자를 입력해달라는 메시지를 출력하고 while문의 처음으로 돌아간다.
사용자가 예측한 숫자(B)rk 1 ~ 100사이 값일 때,
import random # random_num = random.randint(1, 100) restart = 'y' predic_num = 0 restart_count = 0 maximum_count = 0 count = 0 # while random_num != predic_num and restart == 'y': # show last score if maximum_count != 0 and restart_count == 0: print(f'이전 게임 플레이어 최고 시도 점수 : {maximum_count}') restart_count = 1 # # enter prediction number predic_num = input('1 ~ 100 사이 숫자를 맞춰보세요 : ') # try: # user entered integer, and consverts type str to int predic_num = int(predic_num) except: # user entered something other print('자연수를 입력해주세요 (1 ~ 100)') print('') continue # if 1 <= predic_num <= 100: # user enter 1 ~ 100 integer if random_num < predic_num: # predic_num bigger than random number print('다운') count += 1 elif random_num > predic_num: # predic_num smaller than random number print('업') count += 1 else: # prediction is correct count += 1 print(f'맞았습니다.\n 시도한 횟수 : {count}') if maximum_count < count: maximum_count = count count = 0 # # check regame (y or n) restart = input('다시 하시겠습니까? (y/n):') while restart != 'y' and restart != 'n': # user entered something other restart = input('다시 하시겠습니까? (y/n):') # # change random_number and restart_count random_num = random.randint(1, 100) restart_count = 0 else: # user enter bigger than 100 or smaller than 1 print('1 ~ 100사이 자연수를 입력해주세요.') print('')