-
Notifications
You must be signed in to change notification settings - Fork 10
[숫자 야구 게임] 정영인 미션 제출합니다 #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jyi0226
wants to merge
10
commits into
swthewhite-lab:main
Choose a base branch
from
jyi0226:jyi0226
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
67bc20c
Update main.py
jyi0226 588494e
게임 재시작 시 예외사항 수정
jyi0226 afc6754
재시작 함수 개선
jyi0226 97d9fc4
비교 함수 스타일 밑 가독성 개선
jyi0226 e59b512
메인 함수 스타일 및 로직 개선
jyi0226 7fd8bae
게임 함수 스타일 및 로직 개선
jyi0226 76f6f9d
입력 함수 스타일 및 로직 개선
jyi0226 f030d6b
코드 스타일 개선
jyi0226 dc75a7c
전체 코드 스타일 PEP8에 맞게 개선
jyi0226 5413a34
Update main.py
jyi0226 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,89 @@ | ||
| def main(): | ||
| import random | ||
|
|
||
|
|
||
| def check_valid_input(): | ||
| arr_list = list(input("숫자를 입력하세요: ")) | ||
|
|
||
| if len(arr_list) != 3: | ||
| raise ValueError("3 자리 숫자를 입력하세요") | ||
| for i in range(3): | ||
| if not arr_list[i].isdigit(): | ||
| raise ValueError("정수를 입력하세요") | ||
| if int(arr_list[i]) < 1 or int(arr_list[i]) > 9: | ||
| raise ValueError("1부터 9까지의 숫자만 입력하세요") | ||
| if len(arr_list) != len(set(arr_list)): | ||
| raise ValueError("중복된 숫자를 입력하였습니다") | ||
|
|
||
| return [int(x) for x in arr_list] | ||
|
|
||
|
|
||
| def regame(): | ||
| val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.: ") | ||
| if val in ["1", "2"]: | ||
| return int(val) | ||
| else: | ||
| raise ValueError("1 혹은 2를 입력해주세요.") | ||
|
|
||
|
|
||
| def make_num(): | ||
| return random.sample(range(1, 10), 3) | ||
|
|
||
|
|
||
| def input_num(): | ||
| return check_valid_input() | ||
|
|
||
|
|
||
| def check(com, arr): | ||
| """ | ||
| 프로그램의 진입점 함수. | ||
| 여기에서 전체 프로그램 로직을 시작합니다. | ||
| 숫자 야구 게임의 결과를 계산하는 함수 | ||
| - 같은 위치에 같은 숫자가 있으면 스트라이크 | ||
| - 다른 위치에 같은 숫자가 있으면 볼 | ||
| """ | ||
| # 프로그램의 메인 로직을 여기에 구현 | ||
| strikes = 0 | ||
| balls = 0 | ||
|
|
||
| for i in range(3): | ||
| if arr[i] == com[i]: | ||
| strikes += 1 | ||
| else: | ||
| if arr[i] in com: | ||
| balls += 1 | ||
|
|
||
| return strikes, balls | ||
|
|
||
|
|
||
| def game(): | ||
| com = make_num() | ||
|
|
||
| while True: | ||
| arr = input_num() | ||
| strikes, balls = check(com, arr) | ||
|
|
||
| if strikes == 0 and balls == 0: | ||
| print("낫싱") | ||
| elif strikes == 3: | ||
| print("3스트라이크") | ||
| print("3개의 숫자를 모두 맞히셨습니다! 게임 종료") | ||
| break | ||
| else: | ||
| print(f"{balls}볼 {strikes}스트라이크") | ||
|
|
||
|
|
||
| def main(): | ||
| print("숫자 야구 게임을 시작합니다.") | ||
|
|
||
| while True: | ||
| try: | ||
| game() | ||
| choice = regame() | ||
| if choice == 1: | ||
| continue | ||
| elif choice == 2: | ||
| break | ||
| except ValueError as e: | ||
| print(e) | ||
| raise | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # 프로그램이 직접 실행될 때만 main() 함수를 호출 | ||
| main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.