반응형
규칙
# 가위바위보 게임
# 1 : 가위
# 2 : 바위
# 3 : 보
# 컴퓨터는 랜덤으로 가위바위보를 냅니다.
# 사용자는 키보드로 1, 2, 3 중 하나를 입력하여 냅니다.
# 이기면 1점을 획득합니다.
# 먼저 3점을 획득한 사람이 승리합니다.
예상결과

뼈대코드
rock_scissors_paper _뼈대코드.py
0.00MB
''' 가위바위보 게임 1 : 가위 2 : 바위 3 : 보 컴퓨터는 랜덤으로 가위바위보를 냅니다. 사용자는 키보드로 1, 2, 3 중 하나를 입력하여 냅니다. 이기면 1점을 획득합니다. 먼저 3점을 획득한 사람이 승리합니다. ''' name = input('이름을 입력하세요 : ') print('게임을 시작합니다. (가위 : 1, 바위 : 2, 보 : 3') user_score = 0 #사용자 점수 computer_score = 0 #컴퓨터 점수 while True: user = input('가위바위보! : ') computer = '' #여기를 채우세요 #이겼는지 졌는지 확인 print(name, '님이 이겼습니다.') print('컴퓨터가 이겼습니다.') print('비겼습니다.') #현재 점수 알려주기 : '현재 스코어 .... ~~님 : XX점, 컴퓨터 YY점 ... 입니다.' print() #누구라도 3점이 되면 끝난다. #누가 이겼는지 알려주어야 한다. '~~님이 승리하였습니다' / '컴퓨터가 승리하였습니다.'
Hans 의 결과물
import random name = input('이름을 입력하세요 : ') print('게임을 시작합니다. (가위 : 1, 바위 : 2, 보 : 3)') user_score = 0 #사용자 점수 computer_score = 0 #컴퓨터 점수 def lose(): global computer_score computer_score = computer_score + 1 def win(): global user_score user_score = user_score + 1 running = True while running: user_choice = int(float(input('가위바위보! : '))) computer_choice = random.randrange(1, 4) print(user_choice) print(computer_choice) if user_choice == 1: if computer_choice == 1: print('비겼습니다') elif computer_choice == 2: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 3: print('{0}님이 이겼습니다'.format(name)) win() elif user_choice == 2: if computer_choice == 2: print('비겼습니다') elif computer_choice == 3: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 1: print('{0}님이 이겼습니다'.format(name)) win() elif user_choice == 3: if computer_choice == 3: print('비겼습니다') elif computer_choice == 1: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 2: print('{0}님이 이겼습니다'.format(name)) win() #현재 점수 알려주기 : '현재 스코어 .... ~~님 : XX점, 컴퓨터 YY점 ... 입니다.' print('현재 스코어... {0} 님 : {1} 점, 컴퓨터 : {2} 점 입니다'.format(name, user_score, computer_score)) if computer_score >= 3 or user_score >= 3: if computer_score >=3: print('컴퓨터가 승리하였습니다') elif user_score >=3: print('{0}님이 승리하였습니다'.format(name)) running = False quit() #누구라도 3점이 되면 끝난다. #누가 이겼는지 알려주어야 한다. '~~님이 승리하였습니다' / '컴퓨터가 승리하였습니다.'
Feedback
- if- else 구문을 더 단순화할 수 있습니다.
- 비기는 경우는 1, 2, 3 관계없이 user_choice == computer_choice 이면 되기 때문에 통일할 수 있습니다.
- user_choice = int(float(input('가위바위보! : '))) 는 str -> float -> int로 변환합니다.
- user_choice = int(input('가위바위보! : ')) 로 바꿀 수 있습니다.
바꾼 버전 :
#입력 받는 부분 user_choice = int(input('가위바위보! : ')) #승패를 가리는 부분 if user_choice == computer_choice: print('비겼습니다') elif user_choice == 1: if computer_choice == 2: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 3: print('{0}님이 이겼습니다'.format(name)) win() elif user_choice == 2: if computer_choice == 3: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 1: print('{0}님이 이겼습니다'.format(name)) win() elif user_choice == 3: if computer_choice == 1: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 2: print('{0}님이 이겼습니다'.format(name)) win()
Hans의 결과물 2 (응용버전)
# 4 이상의 수를 입력하면 다시 입력을 받습니다 import random import time def delay(x): time.sleep(x) name = input('이름을 입력하세요 : ') delay(0.5) game_round = int(float(input('게임 라운드 수를 입력하세요 : '))) print('규칙 설명') delay(1.5) print('1. 1~3의 숫자를 입력합니다. (가위 : 1, 바위 : 2, 보 : 3)') delay(1.5) print('2. 4 이상의 숫자를 입력했다면 다시 입력하세요.') delay(1.5) print('3. 아무것도 입력하지 않으면 게임이 멈추니 유의해주세요.') delay(1.5) print('4. 먼저 '+ '\033[92m' +'{0}'.format(game_round) + '\033[0m' + ' 점을 획득하시면 승리합니다.') delay(1.5) print('그럼 시작합니다.') delay(1) print('3') delay(1) print('2') delay(1) print('1') delay(1) # name = input('이름을 입력하세요 : ') # print('게임을 시작합니다. (가위 : 1, 바위 : 2, 보 : 3)') user_score = 0 #사용자 점수 computer_score = 0 #컴퓨터 점수 def lose(): global computer_score computer_score = computer_score + 1 def win(): global user_score user_score = user_score + 1 running = True while running: user_choice = int(float(input('가위바위보! : '))) computer_choice = random.randrange(1, 4) if user_choice >3: while user_choice > 3: print('다시 입력해주세요.') user_choice = int(float(input('가위바위보 : '))) delay(0.5) print('{0} : {1}'.format(name, user_choice)) delay(1) print('컴퓨터 : {0}'.format(computer_choice)) delay(1) if user_choice == 1: if computer_choice == 1: print('비겼습니다') elif computer_choice == 2: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 3: print('{0}님이 이겼습니다'.format(name)) win() elif user_choice == 2: if computer_choice == 2: print('비겼습니다') elif computer_choice == 3: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 1: print('{0}님이 이겼습니다'.format(name)) win() elif user_choice == 3: if computer_choice == 3: print('비겼습니다') elif computer_choice == 1: print('컴퓨터가 이겼습니다') lose() elif computer_choice == 2: print('{0}님이 이겼습니다'.format(name)) win() delay(1.2) #현재 점수 알려주기 : '현재 스코어 .... ~~님 : XX점, 컴퓨터 YY점 ... 입니다.' print('현재 스코어... {0} 님 : {1} 점, 컴퓨터 : {2} 점 입니다'.format(name, user_score, computer_score)) if computer_score >= game_round or user_score >= game_round: if computer_score >= game_round: print('컴퓨터가 승리하였습니다') elif user_score >= game_round: print('{0}님이 승리하였습니다'.format(name)) running = False time.sleep(1) quit() #누구라도 3점이 되면 끝난다. #누가 이겼는지 알려주어야 한다. '~~님이 승리하였습니다' / '컴퓨터가 승리하였습니다.'
잘한점
- 규칙을 시간순으로 만들어 가독성이 좋음
- 라운드 수를 직접 입력받도록 함
- 라운드 색깔을 바꾸어 가독성을 높임
- 4이상의 입력값을 주었을 때 예외처리를 해주었음
- win() , lose() 함수를 직접 만들어서 중복을 없애주었음
칭찬의 박수~~~
반응형
'파이썬 | Python > Hans와 함께하는 연습문제' 카테고리의 다른 글
Day2 | 간단한 계산기 (0) | 2020.10.09 |
---|