-
Notifications
You must be signed in to change notification settings - Fork 10
Kimseonggyu03 #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 기능 구현 목록 | ||
| ========== | ||
|
|
||
| **1.숫자를 랜덤으로 생성한다** | ||
| - 단 숫자는 1부터 9까지로 하고 3자리 자연수를 생성,각 자리 숫자는 서로 중복되어선 안된다 | ||
|
|
||
| **2.사용자에게 숫자를 입력받는다** | ||
| - 3자리 자연수가 아니면 "잘못 입력했습니다." 출력 | ||
| - 각 자리 숫자가 서로 중복될 경우 "잘못 입력했습니다." 출력 | ||
| - 1부터 9까지 숫자가 아니면 "잘못 입력했습니다." 출력 | ||
| - 0이하 수가 나올 경우 "잘못 입력했습니다." 출력 | ||
|
|
||
| **3.입력받은 수와 생성된 수를 비교한다** | ||
| - 같은 숫자가 같은 자리에 있을 때:스트라이크 | ||
| - 같은 숫자에 다른 자리에 있을 때:볼 | ||
| - 다른 숫자일 때:낫싱 | ||
|
|
||
| **4.비교 결과를 출력한다** | ||
| - 3스트라이크일때 게임을 종료한다 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,10 +1,58 @@ | ||||||||||||||||||||||||
| def main(): | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| 프로그램의 진입점 함수. | ||||||||||||||||||||||||
| 여기에서 전체 프로그램 로직을 시작합니다. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| # 프로그램의 메인 로직을 여기에 구현 | ||||||||||||||||||||||||
| import random | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def player_input() : | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| player_number = input("숫자를 입력해주세요: ") | ||||||||||||||||||||||||
| if len(player_number) != 3 or not player_number.isdigit(): | ||||||||||||||||||||||||
| raise ValueError("입력은 3자리 숫자여야 합니다.") | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| elif len(set(player_number)) != 3: | ||||||||||||||||||||||||
| raise ValueError("입력 값의 각 자리는 서로 달라야 합니다.") | ||||||||||||||||||||||||
| return player_number | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def game_comp(com_number, player_number) : | ||||||||||||||||||||||||
| strike, ball, index = 0, 0, 0 | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| for i in com_number : | ||||||||||||||||||||||||
| if int(player_number[index]) == i : | ||||||||||||||||||||||||
| strike += 1 | ||||||||||||||||||||||||
| elif int(player_number[index]) in com_number : | ||||||||||||||||||||||||
| ball += 1 | ||||||||||||||||||||||||
| index += 1 | ||||||||||||||||||||||||
| return strike, ball | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def game_print(com_number) : | ||||||||||||||||||||||||
| while True: | ||||||||||||||||||||||||
| number = player_input() | ||||||||||||||||||||||||
| strike, ball = game_comp(com_number, number) | ||||||||||||||||||||||||
| if strike==0 and ball==0: | ||||||||||||||||||||||||
| print("낫싱") | ||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||
| elif strike == 3 : | ||||||||||||||||||||||||
| print(str(strike)+"스트라이크") | ||||||||||||||||||||||||
| print("게임 종료") | ||||||||||||||||||||||||
| break | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| print(str(ball)+"볼", str(strike)+"스트라이크") | ||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def main(): | ||||||||||||||||||||||||
| while True : | ||||||||||||||||||||||||
| com_number = random.sample(range(1,10),3) | ||||||||||||||||||||||||
| print("숫자 야구 게임을 시작합니다.") | ||||||||||||||||||||||||
| game_print(com_number) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| result = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") | ||||||||||||||||||||||||
| if result == '1': | ||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||
| return | ||||||||||||||||||||||||
| elif result == '2': | ||||||||||||||||||||||||
| print("게임을 종료합니다.") | ||||||||||||||||||||||||
| return | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| raise ValueError("잘못된 입력입니다. 1 또는 2를 입력하세요.") | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||
| # 프로그램이 직접 실행될 때만 main() 함수를 호출 | ||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||
|
Comment on lines
57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add global error handling Add try-except block in the main execution to handle unexpected errors gracefully: if __name__ == "__main__":
- main()
+ try:
+ main()
+ except KeyboardInterrupt:
+ print("\n게임이 중단되었습니다.")
+ except Exception as e:
+ print(f"오류가 발생했습니다: {str(e)}")📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove recursive call to prevent stack overflow
The current implementation uses recursion for game restart, which could lead to stack overflow after multiple restarts. Replace with a loop-based approach:
def main(): while True: com_number = random.sample(range(1,10),3) print("숫자 야구 게임을 시작합니다.") game_print(com_number) result = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") if result == '1': - main() - return + continue elif result == '2': print("게임을 종료합니다.") return else: raise ValueError("잘못된 입력입니다. 1 또는 2를 입력하세요.")📝 Committable suggestion