-
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
base: main
Are you sure you want to change the base?
Conversation
""" Walkthrough이번 변경은 Changes
Sequence Diagram(s)sequenceDiagram
participant User as 사용자
participant Main as main()
participant Game as game()
participant Input as input_num()/check_valid_input()
participant Logic as check(), make_num()
User->>Main: 게임 시작 요청
Main->>Game: 게임 실행 호출
Game->>Input: 사용자 입력 요청 및 검증
Input-->>Game: 검증된 입력 반환
Game->>Logic: 난수 생성 및 입력 비교 (strike/ball 계산)
Logic-->>Game: 결과 반환
Game->>User: 결과 출력
User->>Main: 재시작 여부 입력
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 5
🔭 Outside diff range comments (1)
src/baseball/main.py (1)
1-77
: 🛠️ Refactor suggestion전체 코드 PEP8 스타일 가이드 준수 필요
전체 코드에서 다수의 PEP8 스타일 가이드 위반이 발견되었습니다. 파이썬 코드 스타일 가이드(PEP8)를 준수하는 것은 코드의 가독성과 유지보수성을 높이는 데 중요합니다. 다음 사항에 주의하세요:
- 함수 선언 사이에 빈 줄 2개 필요
- 연산자 주변에 공백 추가 필요
- 쉼표 뒤에 공백 추가 필요
- 일관된 들여쓰기 사용 필요
- 키워드 뒤에 공백 추가 필요 (예:
while (1)
또는while True
)- 빈 줄에 불필요한 공백 제거 필요
PEP8 스타일 가이드를 준수하기 위해 자동 포맷팅 도구인
black
이나autopep8
을 사용하는 것을 고려해보세요.파이썬 스타일 가이드에 대한 더 자세한 정보는 다음 링크에서 확인할 수 있습니다:
https://www.python.org/dev/peps/pep-0008/🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 3-3: E302 expected 2 blank lines, found 1
[error] 4-4: E225 missing whitespace around operator
[error] 6-6: E225 missing whitespace around operator
[error] 9-9: E111 indentation is not a multiple of 4
[error] 9-9: E117 over-indented
[error] 10-10: E111 indentation is not a multiple of 4
[error] 17-17: E302 expected 2 blank lines, found 1
[error] 18-18: E225 missing whitespace around operator
[error] 19-19: E231 missing whitespace after ','
[error] 24-24: E302 expected 2 blank lines, found 1
[error] 25-25: E231 missing whitespace after ','
[error] 25-25: E231 missing whitespace after ','
[error] 27-27: E302 expected 2 blank lines, found 1
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 30-30: E231 missing whitespace after ','
[error] 31-31: E225 missing whitespace around operator
[error] 32-32: E225 missing whitespace around operator
[error] 35-35: E225 missing whitespace around operator
[error] 36-36: E225 missing whitespace around operator
[error] 39-39: E225 missing whitespace around operator
[error] 43-43: E302 expected 2 blank lines, found 1
[error] 44-44: E225 missing whitespace around operator
[error] 45-45: W293 blank line contains whitespace
[error] 46-46: E275 missing whitespace after keyword
[error] 47-47: E225 missing whitespace around operator
[error] 48-48: E231 missing whitespace after ','
[error] 50-50: E225 missing whitespace around operator
[error] 50-50: E225 missing whitespace around operator
[error] 52-52: E225 missing whitespace around operator
[error] 58-58: W293 blank line contains whitespace
[error] 59-59: E302 expected 2 blank lines, found 1
[error] 63-63: E275 missing whitespace after keyword
[error] 67-67: E225 missing whitespace around operator
[error] 69-69: E225 missing whitespace around operator
[error] 74-74: W293 blank line contains whitespace
[error] 75-75: E305 expected 2 blank lines after class or function definition, found 1
🧹 Nitpick comments (3)
src/baseball/main.py (3)
24-25
: 무작위 숫자 생성 함수 스타일 개선
make_num()
함수의 PEP8 스타일 문제가 있습니다:
- 쉼표 뒤에 공백이 누락됨 (
range(1,10),3
→range(1, 10), 3
)- 함수 사이에 빈 줄 2개가 필요함
-def make_num(): - return random.sample(range(1,10),3) + + +def make_num(): + return random.sample(range(1, 10), 3)🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 24-24: E302 expected 2 blank lines, found 1
[error] 25-25: E231 missing whitespace after ','
[error] 25-25: E231 missing whitespace after ','
27-28
: 불필요한 래퍼 함수
input_num()
함수는 단순히check_valid_input()
을 호출하는 래퍼 함수입니다. 이 함수가 추가적인 로직을 제공하지 않는다면, 직접check_valid_input()
을 호출하는 것이 코드를 더 간결하게 만들 수 있습니다. 또한 함수 사이에 빈 줄 2개가 누락되었습니다.함수를 유지하려면 최소한 PEP8 스타일 가이드를 준수하도록 수정하세요:
-def input_num(): - return check_valid_input() + + +def input_num(): + return check_valid_input()🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 27-27: E302 expected 2 blank lines, found 1
30-41
: 스트라이크와 볼 판정 로직 명확하게 개선
check()
함수와game()
함수에서 스트라이크와 볼의 규칙이 명확하게 표현되지 않았습니다. 게임 규칙에 익숙하지 않은 사용자를 위해 출력 메시지를 더 명확하게 표현하면 좋겠습니다.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() + print("컴퓨터가 1-9 사이의 서로 다른 숫자 3개를 선택했습니다.") + print("숫자와 위치가 모두 맞으면 스트라이크, 숫자만 맞고 위치가 틀리면 볼입니다.") attempts = 0 while True: attempts += 1 print(f"시도 #{attempts}") arr = input_num() strikes, balls = check(com, arr) if strikes == 0 and balls == 0: - print("낫싱") + print("낫싱 (맞은 숫자가 없습니다)") elif strikes == 3: print("3스트라이크") print(f"3개의 숫자를 모두 맞히셨습니다! (시도 횟수: {attempts})") print(f"정답은 {com} 이었습니다.") break else: - print(f"{balls}볼 {strikes}스트라이크") + result = [] + if balls > 0: + result.append(f"{balls}볼") + if strikes > 0: + result.append(f"{strikes}스트라이크") + print(" ".join(result))Also applies to: 43-58
🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 30-30: E231 missing whitespace after ','
[error] 31-31: E225 missing whitespace around operator
[error] 32-32: E225 missing whitespace around operator
[error] 35-35: E225 missing whitespace around operator
[error] 36-36: E225 missing whitespace around operator
[error] 39-39: E225 missing whitespace around operator
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/baseball/main.py
(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Check PEP8 Style
src/baseball/main.py
[error] 3-3: E302 expected 2 blank lines, found 1
[error] 4-4: E225 missing whitespace around operator
[error] 6-6: E225 missing whitespace around operator
[error] 9-9: E111 indentation is not a multiple of 4
[error] 9-9: E117 over-indented
[error] 10-10: E111 indentation is not a multiple of 4
[error] 17-17: E302 expected 2 blank lines, found 1
[error] 18-18: E225 missing whitespace around operator
[error] 19-19: E231 missing whitespace after ','
[error] 24-24: E302 expected 2 blank lines, found 1
[error] 25-25: E231 missing whitespace after ','
[error] 25-25: E231 missing whitespace after ','
[error] 27-27: E302 expected 2 blank lines, found 1
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 30-30: E231 missing whitespace after ','
[error] 31-31: E225 missing whitespace around operator
[error] 32-32: E225 missing whitespace around operator
[error] 35-35: E225 missing whitespace around operator
[error] 36-36: E225 missing whitespace around operator
[error] 39-39: E225 missing whitespace around operator
[error] 43-43: E302 expected 2 blank lines, found 1
[error] 44-44: E225 missing whitespace around operator
[error] 45-45: W293 blank line contains whitespace
[error] 46-46: E275 missing whitespace after keyword
[error] 47-47: E225 missing whitespace around operator
[error] 48-48: E231 missing whitespace after ','
[error] 50-50: E225 missing whitespace around operator
[error] 50-50: E225 missing whitespace around operator
[error] 52-52: E225 missing whitespace around operator
[error] 58-58: W293 blank line contains whitespace
[error] 59-59: E302 expected 2 blank lines, found 1
[error] 63-63: E275 missing whitespace after keyword
[error] 67-67: E225 missing whitespace around operator
[error] 69-69: E225 missing whitespace around operator
[error] 74-74: W293 blank line contains whitespace
[error] 75-75: E305 expected 2 blank lines after class or function definition, found 1
src/baseball/main.py
Outdated
def regame(): | ||
val=input() | ||
if val in ["1","2"]: | ||
return int(val) | ||
else: | ||
raise ValueError("1혹은 2를 입력해주세요.") |
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.
🛠️ Refactor suggestion
재시작 함수 개선 필요
regame()
함수에 몇 가지 문제가 있습니다:
-
PEP8 스타일 가이드를 준수하지 않습니다:
- 연산자 주변에 공백이 누락됨 (
val=input()
→val = input()
) - 쉼표 뒤에 공백이 누락됨 (
["1","2"]
→["1", "2"]
) - 함수 사이에 빈 줄 2개가 필요함
- 연산자 주변에 공백이 누락됨 (
-
사용자 경험 개선 사항:
input()
함수에 프롬프트 메시지가 없어 사용자가 무엇을 입력해야 하는지 알기 어렵습니다.
-def regame():
- val=input()
- if val in ["1","2"]:
+
+
+def regame():
+ val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요: ")
+ if val in ["1", "2"]:
return int(val)
else:
raise ValueError("1혹은 2를 입력해주세요.")
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
def regame(): | |
val=input() | |
if val in ["1","2"]: | |
return int(val) | |
else: | |
raise ValueError("1혹은 2를 입력해주세요.") | |
def regame(): | |
val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요: ") | |
if val in ["1", "2"]: | |
return int(val) | |
else: | |
raise ValueError("1혹은 2를 입력해주세요.") |
🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 17-17: E302 expected 2 blank lines, found 1
[error] 18-18: E225 missing whitespace around operator
[error] 19-19: E231 missing whitespace after ','
src/baseball/main.py
Outdated
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 |
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.
🛠️ Refactor suggestion
비교 함수 스타일 및 가독성 개선
check()
함수에 여러 스타일 및 가독성 문제가 있습니다:
-
PEP8 스타일 가이드를 준수하지 않습니다:
- 매개변수 사이의 쉼표 뒤에 공백이 누락됨 (
com,arr
→com, arr
) - 연산자 주변에 공백이 누락됨 (
strikes=0
→strikes = 0
) - 함수 사이에 빈 줄 2개가 필요함
- 매개변수 사이의 쉼표 뒤에 공백이 누락됨 (
-
가독성 개선 사항:
- 숫자 야구 게임의 규칙을 설명하는 주석 추가가 필요합니다.
-def check(com,arr):
- strikes=0
- balls=0
+
+
+def check(com, arr):
+ """
+ 숫자 야구 게임의 결과를 계산하는 함수
+ - 같은 위치에 같은 숫자가 있으면 스트라이크
+ - 다른 위치에 같은 숫자가 있으면 볼
+ """
+ strikes = 0
+ balls = 0
for i in range(3):
- if arr[i]==com[i]:
- strikes+=1
+ if arr[i] == com[i]:
+ strikes += 1
else:
if arr[i] in com:
- balls+=1
+ balls += 1
return strikes, balls
🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 30-30: E231 missing whitespace after ','
[error] 31-31: E225 missing whitespace around operator
[error] 32-32: E225 missing whitespace around operator
[error] 35-35: E225 missing whitespace around operator
[error] 36-36: E225 missing whitespace around operator
[error] 39-39: E225 missing whitespace around operator
src/baseball/main.py
Outdated
print("숫자 야구 게임을 시작합니다.") | ||
|
||
while(1): | ||
try: | ||
game() | ||
print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") | ||
if regame()==1: | ||
continue | ||
elif regame()==2: | ||
break | ||
except ValueError as e: | ||
print(e) | ||
break |
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.
🛠️ Refactor suggestion
메인 함수 스타일 및 로직 개선
main()
함수에 여러 스타일 및 로직 문제가 있습니다:
-
PEP8 스타일 가이드를 준수하지 않습니다:
while
키워드 뒤에 공백이 필요함 (while(1)
→while (1)
또는while True
)- 연산자 주변에 공백이 누락됨 (
regame()==1
→regame() == 1
) - 함수 선언 앞에 빈 줄 2개가 필요함
-
로직 개선 사항:
regame()
함수를 두 번 호출하므로 사용자가 입력을 두 번 해야 합니다.- 예외 처리가 모든 ValueError에 대해 게임을 종료시키므로 사용자에게 다시 시도할 기회를 주지 않습니다.
+
+
def main():
print("숫자 야구 게임을 시작합니다.")
- while(1):
+ while True:
try:
game()
print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
- if regame()==1:
+ choice = regame()
+ if choice == 1:
continue
- elif regame()==2:
+ elif choice == 2:
break
except ValueError as e:
print(e)
- break
+ continue
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
print("숫자 야구 게임을 시작합니다.") | |
while(1): | |
try: | |
game() | |
print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") | |
if regame()==1: | |
continue | |
elif regame()==2: | |
break | |
except ValueError as e: | |
print(e) | |
break | |
def main(): | |
print("숫자 야구 게임을 시작합니다.") | |
while True: | |
try: | |
game() | |
print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") | |
choice = regame() | |
if choice == 1: | |
continue | |
elif choice == 2: | |
break | |
except ValueError as e: | |
print(e) | |
continue |
🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 63-63: E275 missing whitespace after keyword
[error] 67-67: E225 missing whitespace around operator
[error] 69-69: E225 missing whitespace around operator
src/baseball/main.py
Outdated
def game(): | ||
com=make_num() | ||
|
||
while(1): | ||
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}스트라이크") | ||
|
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.
🛠️ Refactor suggestion
게임 함수 스타일 및 로직 개선
game()
함수에 여러 스타일 및 로직 문제가 있습니다:
-
PEP8 스타일 가이드를 준수하지 않습니다:
- 연산자 주변에 공백이 누락됨 (
com=make_num()
→com = make_num()
) - 빈 줄에 공백이 포함됨 (45줄)
while
키워드 뒤에 공백이 필요함 (while(1)
→while (1)
)- 매개변수 사이의 쉼표 뒤에 공백이 누락됨 (
com,arr
→com, arr
) - 비교 연산자 주변에 공백이 누락됨 (
strikes==0
→strikes == 0
) - 함수 사이에 빈 줄 2개가 필요함
- 연산자 주변에 공백이 누락됨 (
-
로직 개선 사항:
- 시도 횟수 제한이나 정보 표시가 없습니다.
- 사용자가 승리한 후 정답(컴퓨터의 숫자)을 보여주는 것이 좋습니다.
-def game():
- com=make_num()
-
- while(1):
- arr=input_num()
- strikes, balls = check(com,arr)
+
+
+def game():
+ com = make_num()
+ attempts = 0
+
+ while True:
+ attempts += 1
+ print(f"시도 #{attempts}")
+ arr = input_num()
+ strikes, balls = check(com, arr)
- if strikes==0 and balls==0:
+ if strikes == 0 and balls == 0:
print("낫싱")
- elif strikes==3:
+ elif strikes == 3:
print("3스트라이크")
- print("3개의 숫자를 모두 맞히셨습니다! 게임 종료")
+ print(f"3개의 숫자를 모두 맞히셨습니다! (시도 횟수: {attempts})")
+ print(f"정답은 {com} 이었습니다.")
break
else:
print(f"{balls}볼 {strikes}스트라이크")
-
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
def game(): | |
com=make_num() | |
while(1): | |
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 game(): | |
com = make_num() | |
attempts = 0 | |
while True: | |
attempts += 1 | |
print(f"시도 #{attempts}") | |
arr = input_num() | |
strikes, balls = check(com, arr) | |
if strikes == 0 and balls == 0: | |
print("낫싱") | |
elif strikes == 3: | |
print("3스트라이크") | |
print(f"3개의 숫자를 모두 맞히셨습니다! (시도 횟수: {attempts})") | |
print(f"정답은 {com} 이었습니다.") | |
break | |
else: | |
print(f"{balls}볼 {strikes}스트라이크") |
🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 43-43: E302 expected 2 blank lines, found 1
[error] 44-44: E225 missing whitespace around operator
[error] 45-45: W293 blank line contains whitespace
[error] 46-46: E275 missing whitespace after keyword
[error] 47-47: E225 missing whitespace around operator
[error] 48-48: E231 missing whitespace after ','
[error] 50-50: E225 missing whitespace around operator
[error] 50-50: E225 missing whitespace around operator
[error] 52-52: E225 missing whitespace around operator
[error] 58-58: W293 blank line contains whitespace
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.
Actionable comments posted: 0
♻️ Duplicate comments (2)
src/baseball/main.py (2)
1-15
:⚠️ Potential issue함수 스타일과 입력 검증 로직 개선이 필요합니다.
check_valid_input()
함수에 여러 가지 스타일 및 로직 문제가 있습니다:
PEP8 스타일 가이드 위반이 다수 있습니다:
- 함수 정의 전에 빈 줄 2개가 필요합니다
- 연산자 주변 공백이 필요합니다 (
arr_list=list
→arr_list = list
,len(arr_list)!=3
→len(arr_list) != 3
)- 들여쓰기가 일관되지 않고 과도하게 사용되었습니다 (9-10줄)
로직 개선 사항:
- 입력값이 1부터 9 사이의 숫자인지 검증하는 로직이 없습니다
- 사용자가 입력할 때 어떤 숫자를 입력해야 하는지 명확하게 알려주는 메시지가 필요합니다
다음과 같이 코드를 수정해보세요:
import random + + def check_valid_input(): - arr_list=list(input("숫자를 입력하세요: ")) + arr_list = list(input("1에서 9까지의 서로 다른 숫자 3개를 입력하세요: ")) - if len(arr_list)!=3: + if len(arr_list) != 3: raise ValueError("3 자리 숫자를 입력하세요") for i in range(3): - if not arr_list[i].isdigit(): - raise ValueError("정수를 입력하세요") + 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]🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 3-3: E302 expected 2 blank lines, found 1
[error] 4-4: E225 missing whitespace around operator
[error] 6-6: E225 missing whitespace around operator
[error] 9-9: E111 indentation is not a multiple of 4
[error] 9-9: E117 over-indented
[error] 10-10: E111 indentation is not a multiple of 4
64-77
:⚠️ Potential issue메인 함수 스타일 및 사용자 경험 개선이 필요합니다.
main()
함수에 여러 가지 스타일 및 사용자 경험 문제가 있습니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 정의 전에 빈 줄 2개가 필요합니다
while
키워드 뒤에 공백이 필요합니다 (while(1)
→while (1)
또는 더 좋게는while True
)- 연산자 주변에 공백이 필요합니다 (
regame()==1
→regame() == 1
)로직 및 사용자 경험 문제:
regame()
함수를 두 번 호출하므로 사용자가 입력을 두 번 해야 합니다- 예외 처리 시
break
를 사용하여 게임이 완전히 종료됩니다. 단순한 입력 오류의 경우 게임을 계속할 수 있도록continue
를 사용하는 것이 좋습니다- swthewhite님의 코멘트처럼 예외 처리 부분에서 향후 상위 호출자에게 예외를 전달해야 할 경우를 대비해 예외를 다시 발생시켜야 합니다
다음과 같이 수정하세요:
+ + def main(): print("숫자 야구 게임을 시작합니다.") - while(1): + while True: try: game() - if regame()==1: + print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") + choice = regame() + if choice == 1: continue - elif regame()==2: + elif choice == 2: break except ValueError as e: print(e) - break + continue + # raise # 필요한 경우 예외를 다시 발생시킬 수 있습니다🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 64-64: E302 expected 2 blank lines, found 1
[error] 68-68: E275 missing whitespace after keyword
[error] 71-71: E225 missing whitespace around operator
[error] 73-73: E225 missing whitespace around operator
🧹 Nitpick comments (2)
src/baseball/main.py (2)
24-25
: 랜덤 숫자 생성 함수 스타일 개선이 필요합니다.
make_num()
함수에 스타일 문제가 있습니다:
- PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 정의 전에 빈 줄 2개가 필요합니다
- 쉼표 뒤에 공백이 필요합니다 (
range(1,10),3
→range(1, 10), 3
)다음과 같이 수정하세요:
+ + def make_num(): - return random.sample(range(1,10),3) + return random.sample(range(1, 10), 3)🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 24-24: E302 expected 2 blank lines, found 1
[error] 25-25: E231 missing whitespace after ','
[error] 25-25: E231 missing whitespace after ','
27-28
: 입력 함수 스타일 개선 및 효율성 검토가 필요합니다.
input_num()
함수에 스타일 문제가 있습니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 정의 전에 빈 줄 2개가 필요합니다
효율성 문제:
- 이 함수는 단순히
check_valid_input()
을 감싸고 있을 뿐입니다. 이 경우 래퍼 함수를 제거하고 직접 호출하는 것이 코드를 더 명확하게 만들 수 있습니다.다음과 같이 수정하거나 함수를 유지해야 할 경우 스타일을 개선하세요:
+ + def input_num(): return check_valid_input()🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 27-27: E302 expected 2 blank lines, found 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/baseball/main.py
(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Check PEP8 Style
src/baseball/main.py
[error] 3-3: E302 expected 2 blank lines, found 1
[error] 4-4: E225 missing whitespace around operator
[error] 6-6: E225 missing whitespace around operator
[error] 9-9: E111 indentation is not a multiple of 4
[error] 9-9: E117 over-indented
[error] 10-10: E111 indentation is not a multiple of 4
[error] 17-17: E302 expected 2 blank lines, found 1
[error] 19-19: E231 missing whitespace after ','
[error] 24-24: E302 expected 2 blank lines, found 1
[error] 25-25: E231 missing whitespace after ','
[error] 25-25: E231 missing whitespace after ','
[error] 27-27: E302 expected 2 blank lines, found 1
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 36-36: E225 missing whitespace around operator
[error] 37-37: E225 missing whitespace around operator
[error] 48-48: E302 expected 2 blank lines, found 1
[error] 49-49: E225 missing whitespace around operator
[error] 50-50: W293 blank line contains whitespace
[error] 51-51: E275 missing whitespace after keyword
[error] 52-52: E225 missing whitespace around operator
[error] 53-53: E231 missing whitespace after ','
[error] 55-55: E225 missing whitespace around operator
[error] 55-55: E225 missing whitespace around operator
[error] 57-57: E225 missing whitespace around operator
[error] 63-63: W293 blank line contains whitespace
[error] 64-64: E302 expected 2 blank lines, found 1
[error] 68-68: E275 missing whitespace after keyword
[error] 71-71: E225 missing whitespace around operator
[error] 73-73: E225 missing whitespace around operator
[error] 78-78: W293 blank line contains whitespace
[error] 79-79: E305 expected 2 blank lines after class or function definition, found 1
🔇 Additional comments (3)
src/baseball/main.py (3)
17-22
: 재시작 함수의 스타일 및 가독성 개선이 필요합니다.
regame()
함수에 몇 가지 스타일 문제가 있습니다:
- PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 정의 전에 빈 줄 2개가 필요합니다
- 쉼표 뒤에 공백이 필요합니다 (
["1","2"]
→["1", "2"]
)다음과 같이 수정하세요:
+ + def regame(): val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.: ") - if val in ["1","2"]: + if val in ["1", "2"]: return int(val) else: raise ValueError("1혹은 2를 입력해주세요.")🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 17-17: E302 expected 2 blank lines, found 1
[error] 19-19: E231 missing whitespace after ','
30-46
: 비교 함수 스타일 및 가독성 개선이 필요합니다.
check()
함수에 여러 가지 스타일 문제가 있습니다:
- PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 정의 전에 빈 줄 2개가 필요합니다
- 연산자 주변에 공백이 필요합니다 (
strikes=0
→strikes = 0
,balls=0
→balls = 0
)도움이 되는 주석이 추가되었지만, 코드 스타일 문제가 여전히 남아있습니다.
다음과 같이 수정하세요:
+ + def check(com, arr): """ 숫자 야구 게임의 결과를 계산하는 함수 - 같은 위치에 같은 숫자가 있으면 스트라이크 - 다른 위치에 같은 숫자가 있으면 볼 """ - strikes=0 - balls=0 + 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🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 36-36: E225 missing whitespace around operator
[error] 37-37: E225 missing whitespace around operator
48-63
: 게임 함수 스타일 및 사용자 경험 개선이 필요합니다.
game()
함수에 여러 가지 스타일 및 사용자 경험 문제가 있습니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 정의 전에 빈 줄 2개가 필요합니다
- 연산자 주변에 공백이 필요합니다 (
com=make_num()
→com = make_num()
)while
키워드 뒤에 공백이 필요합니다 (while(1)
→while (1)
또는 더 좋게는while True
)- 쉼표 뒤에 공백이 필요합니다 (
com,arr
→com, arr
)- 연산자 주변에 공백이 필요합니다 (
strikes==0
→strikes == 0
)- 빈 줄에 불필요한 공백이 있습니다
사용자 경험 개선 사항:
- 시도 횟수를 보여주는 카운터가 없습니다
- 게임이 끝났을 때 정답을 보여주지 않습니다
다음과 같이 개선하세요:
+ + def game(): - com=make_num() - - while(1): - arr=input_num() - strikes, balls = check(com,arr) + com = make_num() + attempts = 0 + + while True: + attempts += 1 + print(f"시도 #{attempts}") + arr = input_num() + strikes, balls = check(com, arr) - if strikes==0 and balls==0: + if strikes == 0 and balls == 0: print("낫싱") - elif strikes==3: + elif strikes == 3: print("3스트라이크") - print("3개의 숫자를 모두 맞히셨습니다! 게임 종료") + print(f"3개의 숫자를 모두 맞히셨습니다! (시도 횟수: {attempts})") + print(f"정답은 {com} 이었습니다.") break else: print(f"{balls}볼 {strikes}스트라이크") -🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 48-48: E302 expected 2 blank lines, found 1
[error] 49-49: E225 missing whitespace around operator
[error] 50-50: W293 blank line contains whitespace
[error] 51-51: E275 missing whitespace after keyword
[error] 52-52: E225 missing whitespace around operator
[error] 53-53: E231 missing whitespace after ','
[error] 55-55: E225 missing whitespace around operator
[error] 55-55: E225 missing whitespace around operator
[error] 57-57: E225 missing whitespace around operator
[error] 63-63: W293 blank line contains whitespace
Codecov ReportAttention: Patch coverage is
🚀 New features to boost your workflow:
|
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.
Actionable comments posted: 0
♻️ Duplicate comments (2)
src/baseball/main.py (2)
3-15
: 🛠️ Refactor suggestion코드 스타일 및 유효성 검증 개선 필요
check_valid_input()
함수에 몇 가지 개선이 필요합니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 전에 빈 줄 2개가 필요합니다
- 연산자 주변에 공백이 누락되었습니다 (
arr_list=list
→arr_list = list
)- 들여쓰기가 일관되지 않고 과도합니다 (9-10줄)
입력 검증 로직이 불완전합니다:
- 입력된 숫자가 1-9 범위인지 확인하는 검증이 없습니다
- 함수의 목적을 설명하는 docstring이 없습니다
아래 수정사항을 적용해주세요:
-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 len(arr_list) != len(set(arr_list)): - raise ValueError("중복된 숫자를 입력하였습니다") - - return [int(x) for x in arr_list] + + +def check_valid_input(): + """ + 사용자로부터 3개의 서로 다른 1-9 사이의 숫자를 입력받고 유효성을 검사합니다. + + Returns: + list: 유효한 3개의 정수 리스트 + Raises: + ValueError: 입력이 유효하지 않을 경우 + """ + 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]🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 10-10: src/baseball/main.py#L10
Added line #L10 was not covered by tests
[warning] 13-13: src/baseball/main.py#L13
Added line #L13 was not covered by tests🪛 GitHub Actions: Check PEP8 Style
[error] 3-3: E302 expected 2 blank lines, found 1
[error] 4-4: E225 missing whitespace around operator
[error] 6-6: E225 missing whitespace around operator
[error] 9-9: E111 indentation is not a multiple of 4
[error] 9-9: E117 over-indented
[error] 10-10: E111 indentation is not a multiple of 4
48-63
: 🛠️ Refactor suggestion게임 함수 스타일 및 로직 개선
game()
함수에 여러 스타일 및 로직 문제가 있습니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 전에 빈 줄 2개가 필요합니다
- 연산자 주변에 공백이 누락되었습니다 (
com=make_num()
→com = make_num()
)- 빈 줄에 공백이 포함되었습니다 (50줄)
while
키워드 뒤에 공백이 필요합니다 (while(1)
→while (1)
또는while True
)- 쉼표 뒤에 공백이 누락되었습니다 (
com,arr
→com, arr
)- 비교 연산자 주변에 공백이 누락되었습니다 (
strikes==0
→strikes == 0
)로직 개선 사항:
- 시도 횟수를 추적하고 표시하는 기능이 없습니다
- 게임 승리 후 컴퓨터가 선택한 정답을 보여주지 않습니다
- 함수의 목적을 설명하는 docstring이 없습니다
아래 수정사항을 적용해주세요:
-def game(): - com=make_num() - - while(1): - 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 game(): + """ + 숫자 야구 게임의 주요 로직을 처리하는 함수 + """ + com = make_num() + attempts = 0 + + while True: + attempts += 1 + print(f"시도 #{attempts}") + arr = input_num() + strikes, balls = check(com, arr) + + if strikes == 0 and balls == 0: + print("낫싱") + elif strikes == 3: + print("3스트라이크") + print(f"3개의 숫자를 모두 맞히셨습니다! (시도 횟수: {attempts})") + print(f"정답은 {com} 이었습니다.") + break + else: + print(f"{balls}볼 {strikes}스트라이크")🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 48-48: E302 expected 2 blank lines, found 1
[error] 49-49: E225 missing whitespace around operator
[error] 50-50: W293 blank line contains whitespace
[error] 51-51: E275 missing whitespace after keyword
[error] 52-52: E225 missing whitespace around operator
[error] 53-53: E231 missing whitespace after ','
[error] 55-55: E225 missing whitespace around operator
[error] 55-55: E225 missing whitespace around operator
[error] 57-57: E225 missing whitespace around operator
[error] 63-63: W293 blank line contains whitespace
🧹 Nitpick comments (2)
src/baseball/main.py (2)
24-25
: 난수 생성 함수 스타일 개선 필요
make_num()
함수에 몇 가지 개선이 필요합니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 전에 빈 줄 2개가 필요합니다
- 쉼표 뒤에 공백이 누락되었습니다 (
range(1,10),3
→range(1, 10), 3
)함수의 목적을 설명하는 docstring이 없습니다
아래 수정사항을 적용해주세요:
-def make_num(): - return random.sample(range(1,10),3) + + +def make_num(): + """ + 숫자 야구 게임에 사용할 3개의 서로 다른 랜덤 숫자(1-9)를 생성합니다. + + Returns: + list: 3개의 서로 다른 랜덤 숫자 리스트 + """ + return random.sample(range(1, 10), 3)🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 24-24: E302 expected 2 blank lines, found 1
[error] 25-25: E231 missing whitespace after ','
[error] 25-25: E231 missing whitespace after ','
27-28
: 입력 함수 불필요한 중첩
input_num()
함수는 단순히check_valid_input()
을 호출하기만 합니다. 이 추가 계층이 꼭 필요한지 검토해보세요.
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 전에 빈 줄 2개가 필요합니다
함수의 목적을 설명하는 docstring이 없습니다
아래 수정사항을 적용하거나, 더 나은 방법으로 이 함수를 개선해주세요:
-def input_num(): - return check_valid_input() + + +def input_num(): + """ + 사용자로부터 유효한 숫자를 입력받습니다. + + Returns: + list: 3개의 유효한 숫자 리스트 + Raises: + ValueError: 입력이 유효하지 않을 경우 + """ + return check_valid_input()🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 27-27: E302 expected 2 blank lines, found 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/baseball/main.py
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/baseball/main.py
[warning] 10-10: src/baseball/main.py#L10
Added line #L10 was not covered by tests
[warning] 13-13: src/baseball/main.py#L13
Added line #L13 was not covered by tests
[warning] 22-22: src/baseball/main.py#L22
Added line #L22 was not covered by tests
🪛 GitHub Actions: Check PEP8 Style
src/baseball/main.py
[error] 3-3: E302 expected 2 blank lines, found 1
[error] 4-4: E225 missing whitespace around operator
[error] 6-6: E225 missing whitespace around operator
[error] 9-9: E111 indentation is not a multiple of 4
[error] 9-9: E117 over-indented
[error] 10-10: E111 indentation is not a multiple of 4
[error] 17-17: E302 expected 2 blank lines, found 1
[error] 19-19: E231 missing whitespace after ','
[error] 24-24: E302 expected 2 blank lines, found 1
[error] 25-25: E231 missing whitespace after ','
[error] 25-25: E231 missing whitespace after ','
[error] 27-27: E302 expected 2 blank lines, found 1
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 36-36: E225 missing whitespace around operator
[error] 37-37: E225 missing whitespace around operator
[error] 48-48: E302 expected 2 blank lines, found 1
[error] 49-49: E225 missing whitespace around operator
[error] 50-50: W293 blank line contains whitespace
[error] 51-51: E275 missing whitespace after keyword
[error] 52-52: E225 missing whitespace around operator
[error] 53-53: E231 missing whitespace after ','
[error] 55-55: E225 missing whitespace around operator
[error] 55-55: E225 missing whitespace around operator
[error] 57-57: E225 missing whitespace around operator
[error] 63-63: W293 blank line contains whitespace
[error] 64-64: E302 expected 2 blank lines, found 1
[error] 79-79: W293 blank line contains whitespace
[error] 80-80: E305 expected 2 blank lines after class or function definition, found 1
🔇 Additional comments (3)
src/baseball/main.py (3)
17-22
: 재시작 함수 스타일 개선 필요
regame()
함수에 몇 가지 개선이 필요합니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 전에 빈 줄 2개가 필요합니다
- 쉼표 뒤에 공백이 누락되었습니다 (
["1","2"]
→["1", "2"]
)사용자 입력 프롬프트는 이전 리뷰를 반영하여 개선되었지만, 끝에 마침표가 있어 일관성이 떨어집니다.
아래 수정사항을 적용해주세요:
-def regame(): - val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.: ") - if val in ["1","2"]: - return int(val) - else: - raise ValueError("1혹은 2를 입력해주세요.") + + +def regame(): + """ + 게임을 재시작할지 종료할지 사용자 입력을 받는 함수 + + Returns: + int: 1은 재시작, 2는 종료 + Raises: + ValueError: 유효하지 않은 입력인 경우 + """ + val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요: ") + if val in ["1", "2"]: + return int(val) + else: + raise ValueError("1혹은 2를 입력해주세요.")🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 22-22: src/baseball/main.py#L22
Added line #L22 was not covered by tests🪛 GitHub Actions: Check PEP8 Style
[error] 17-17: E302 expected 2 blank lines, found 1
[error] 19-19: E231 missing whitespace after ','
30-46
: 비교 함수 스타일 및 가독성 개선
check()
함수에 몇 가지 개선이 필요합니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 전에 빈 줄 2개가 필요합니다
- 연산자 주변에 공백이 누락되었습니다 (
strikes=0
→strikes = 0
)함수가 docstring을 가지고 있어 좋습니다만, 매개변수와 반환값에 대한 설명이 없습니다
아래 수정사항을 적용해주세요:
-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 check(com, arr): + """ + 숫자 야구 게임의 결과를 계산하는 함수 + - 같은 위치에 같은 숫자가 있으면 스트라이크 + - 다른 위치에 같은 숫자가 있으면 볼 + + Args: + com (list): 컴퓨터가 생성한 3개의 숫자 리스트 + arr (list): 사용자가 입력한 3개의 숫자 리스트 + + Returns: + tuple: (스트라이크 개수, 볼 개수) + """ + 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🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 30-30: E302 expected 2 blank lines, found 1
[error] 36-36: E225 missing whitespace around operator
[error] 37-37: E225 missing whitespace around operator
64-79
:❓ Verification inconclusive
메인 함수 스타일 및 예외 처리 개선
main()
함수에 몇 가지 개선이 필요합니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 전에 빈 줄 2개가 필요합니다
- 마지막 빈 줄에 공백이 포함되었습니다 (79줄)
예외 처리에 있어서 문제가 있습니다:
- 77-78줄에서 예외를 출력한 후 다시 발생시키는데(
raise
), 이렇게 하면 예외가 상위 레벨로 전파되어 프로그램이 종료됩니다- swthewhite님의 코멘트에 따르면 이는 의도된 것일 수 있으나, 사용자 경험 측면에서는 개선이 필요합니다
아래 수정사항을 적용해주세요:
-def main(): - - print("숫자 야구 게임을 시작합니다.") - - while True: - try: - game() - choice = regame() - if choice == 1: - continue - elif choice == 2: - break - except ValueError as e: - print(e) - raise - + + +def main(): + """ + 숫자 야구 게임의 메인 함수 + """ + print("숫자 야구 게임을 시작합니다.") + + while True: + try: + game() + choice = regame() + if choice == 1: + continue + elif choice == 2: + break + except ValueError as e: + print(e) + print("게임을 다시 시작합니다.") + continueswthewhite님의 코멘트에 따라
raise
를 유지해야 한다면, 적절한 예외 처리 방법을 설명해주시기 바랍니다. 현재 코드는 예외 발생 시 프로그램이 종료되는 문제가 있습니다.
메인 함수 스타일 및 예외 처리 관련 검증 및 제안
PEP8 규칙 준수:
- 함수 선언 전에 빈 줄 2개가 없으며, 마지막 빈 줄에 불필요한 공백이 포함되어 있습니다.
예외 처리 방식:
- 현재
except ValueError as e
블록은 예외를 출력한 후raise
를 사용해 예외를 다시 발생시키므로, 예외 발생 시 게임이 종료됩니다.- 만약 swthewhite님의 코멘트대로 예외 발생시 디버깅이나 로깅 목적에서
raise
를 유지하려면, 예외를 사용자에게 안내한 후 프로그램 종료로 이어지는 것이 맞습니다.- 반면, 사용자 경험 측면에서는 게임을 계속 진행시키거나 재시작하도록 하는 방법(예:
continue
로 처리)도 고려해볼 만합니다.제안된 수정(diff 예시):
-def main(): - - print("숫자 야구 게임을 시작합니다.") - - while True: - try: - game() - choice = regame() - if choice == 1: - continue - elif choice == 2: - break - except ValueError as e: - print(e) - raise +def main(): + """ + 숫자 야구 게임의 메인 함수 + """ + print("숫자 야구 게임을 시작합니다.") + + while True: + try: + game() + choice = regame() + if choice == 1: + continue + elif choice == 2: + break + except ValueError as e: + print(e) + print("게임을 다시 시작합니다.") + continue추가 설명:
- 만약
raise
를 유지하는 것이 의도된 동작(예: 오류 발생 시 프로그램 전체를 종료하여 문제를 드러내기 위함)이면, 그 의도를 명확히 하기 위해 주석이나 로그 메시지에 해당 이유를 명시하는 것이 좋습니다.- 반대로 사용자 경험 개선이 주 목표라면, 예외 발생 시 사용자에게 친숙한 에러 안내와 함께 게임 재시작 로직을 적용하는 것을 권장합니다.
🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 64-64: E302 expected 2 blank lines, found 1
[error] 79-79: W293 blank line contains whitespace
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.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/baseball/main.py (1)
57-72
: 🛠️ Refactor suggestion게임 함수 스타일 및 가독성 개선 필요
게임 함수에 여러 스타일 및 가독성 문제가 있습니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 빈 줄에 공백이 포함됨 (59줄)
while
키워드 뒤에 공백이 필요함 (while(1)
→while (1)
또는while True
)- 함수의 목적을 설명하는 docstring이 없음
기능적 개선사항:
- 사용자의 시도 횟수를 추적하고 표시하는 기능이 없음
- 사용자가 정답을 맞혔을 때 컴퓨터가 생성한 정답을 표시하지 않음
다음과 같이 개선하는 것을 권장합니다:
def game(): + """ + 숫자 야구 게임의 메인 로직을 처리하는 함수 + - 무작위 숫자를 생성하고 사용자의 입력을 받아 결과를 계산 + - 사용자가 3스트라이크를 달성할 때까지 게임 진행 + """ com = make_num() - - while(1): + attempts = 0 + + while True: + attempts += 1 + print(f"시도 #{attempts}") arr = input_num() strikes, balls = check(com, arr) if strikes == 0 and balls == 0: print("낫싱") elif strikes == 3: print("3스트라이크") - print("3개의 숫자를 모두 맞히셨습니다! 게임 종료") + print(f"3개의 숫자를 모두 맞히셨습니다! 게임 종료 (시도 횟수: {attempts})") + print(f"정답은 {com} 이었습니다.") break else: print(f"{balls}볼 {strikes}스트라이크")🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[warning] 59-59: W293 blank line contains whitespace
[warning] 60-60: E275 missing whitespace after keyword
🧹 Nitpick comments (4)
src/baseball/main.py (4)
3-16
: 입력 검증 함수 개선 필요입력 검증 함수에 몇 가지 개선이 필요합니다:
PEP8 스타일 가이드를 준수하지 않는 문제:
- 함수 선언 앞에 빈 줄 2개가 필요함 (1줄만 있음)
기능적 개선사항:
- 함수의 목적을 설명하는 docstring이 없음
- 숫자 범위 검사(1-9)는 좋지만, 테스트 커버리지가 부족함
다음과 같이 개선하는 것을 권장합니다:
import random + def check_valid_input(): + """ + 사용자로부터 입력받은 값이 유효한지 검사하는 함수 + - 3자리 숫자인지 확인 + - 각 자리가 1-9 사이의 숫자인지 확인 + - 중복된 숫자가 없는지 확인 + + Returns: + list: 유효한 입력값을 정수 리스트로 변환하여 반환 + Raises: + ValueError: 유효하지 않은 입력일 경우 발생 + """ arr_list = list(input("숫자를 입력하세요: ")) if len(arr_list) != 3:🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 10-10: src/baseball/main.py#L10
Added line #L10 was not covered by tests
[warning] 12-12: src/baseball/main.py#L12
Added line #L12 was not covered by tests
[warning] 14-14: src/baseball/main.py#L14
Added line #L14 was not covered by tests🪛 GitHub Actions: Check PEP8 Style
[warning] 3-3: E302 expected 2 blank lines, found 1
28-29
: 난수 생성 함수 개선 필요난수 생성 함수에 PEP8 스타일 위반 사항이 있습니다:
- 함수 선언 앞에 빈 줄이 너무 많음 (2개 필요, 3개 있음)
- 쉼표 뒤에 공백이 누락됨 (
range(1,10)
→range(1, 10)
)- 함수의 목적을 설명하는 docstring이 없음
다음과 같이 개선하는 것을 권장합니다:
- def make_num(): - return random.sample(range(1,10), 3) + """ + 게임에서 사용할 3자리 무작위 숫자를 생성하는 함수 + + Returns: + list: 1부터 9까지의 서로 다른 숫자 3개로 구성된 리스트 + """ + return random.sample(range(1, 10), 3)🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[warning] 28-28: E303 too many blank lines (3)
[warning] 29-29: E231 missing whitespace after ','
33-34
: 입력 함수 개선 필요입력 함수에 다음과 같은 문제가 있습니다:
- 함수 선언 앞에 빈 줄이 너무 많음 (2개 필요, 3개 있음)
- 함수의 목적을 설명하는 docstring이 없음
- 이 함수는 단순히 다른 함수를 호출하기만 하므로 불필요한 래핑일 수 있음
다음과 같이 개선하거나 함수를 제거하는 것을 고려해 보세요:
- def input_num(): + """ + 사용자로부터 유효한 입력을 받아 반환하는 함수 + + Returns: + list: 유효한 입력값을 정수 리스트로 변환하여 반환 + Raises: + ValueError: 유효하지 않은 입력일 경우 발생 + """ return check_valid_input()🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[warning] 33-33: E303 too many blank lines (3)
75-90
: 메인 함수 스타일 및 오류 처리 개선 필요메인 함수에 여러 스타일 및 오류 처리 문제가 있습니다:
PEP8 스타일 가이드를 준수하지 않습니다:
- 함수 선언 앞에 빈 줄이 너무 많음 (2개 필요, 3개 있음)
- 함수 끝에 공백이 포함된 빈 줄이 있음 (90줄)
- 함수 정의 이후 필요한 빈 줄 수가 부족함 (2개 필요, 1개 있음)
오류 처리 개선사항:
- 예외 발생 시 다시 raise하는 것은 프로그램을 종료시킵니다. 이는 swthewhite의 코멘트와 일치하지만, 사용자 경험 측면에서는 게임을 계속할 기회를 주는 것이 더 나을 수 있습니다.
다음과 같이 개선하는 것을 권장합니다:
- 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__":🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[warning] 75-75: E303 too many blank lines (3)
[warning] 90-90: W293 blank line contains whitespace
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/baseball/main.py
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/baseball/main.py
[warning] 10-10: src/baseball/main.py#L10
Added line #L10 was not covered by tests
[warning] 12-12: src/baseball/main.py#L12
Added line #L12 was not covered by tests
[warning] 14-14: src/baseball/main.py#L14
Added line #L14 was not covered by tests
[warning] 24-24: src/baseball/main.py#L24
Added line #L24 was not covered by tests
🪛 GitHub Actions: Check PEP8 Style
src/baseball/main.py
[warning] 3-3: E302 expected 2 blank lines, found 1
[warning] 21-21: E231 missing whitespace after ','
[warning] 28-28: E303 too many blank lines (3)
[warning] 29-29: E231 missing whitespace after ','
[warning] 33-33: E303 too many blank lines (3)
[warning] 38-38: E303 too many blank lines (3)
[warning] 44-44: E225 missing whitespace around operator
[warning] 45-45: E225 missing whitespace around operator
[warning] 59-59: W293 blank line contains whitespace
[warning] 60-60: E275 missing whitespace after keyword
[warning] 75-75: E303 too many blank lines (3)
[warning] 90-90: W293 blank line contains whitespace
[warning] 91-91: E305 expected 2 blank lines after class or function definition, found 1
🔇 Additional comments (3)
src/baseball/main.py (3)
19-24
: 재시작 함수 스타일 개선 필요재시작 함수에 PEP8 스타일 위반 사항이 있습니다:
- 쉼표 뒤에 공백이 누락됨 (
["1","2"]
→["1", "2"]
)- 테스트 커버리지가 부족함 (예외 발생 경로)
다음과 같이 개선하는 것을 권장합니다:
def regame(): val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.: ") - if val in ["1","2"]: + if val in ["1", "2"]: return int(val) else: raise ValueError("1혹은 2를 입력해주세요.")🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 24-24: src/baseball/main.py#L24
Added line #L24 was not covered by tests🪛 GitHub Actions: Check PEP8 Style
[warning] 21-21: E231 missing whitespace after ','
38-54
: 비교 함수 스타일 개선 필요비교 함수에 PEP8 스타일 위반 사항이 있습니다:
- 함수 선언 앞에 빈 줄이 너무 많음 (2개 필요, 3개 있음)
- 연산자 주변에 공백이 누락됨 (
strikes=0
→strikes = 0
,balls=0
→balls = 0
)다음과 같이 개선하는 것을 권장합니다:
- def check(com, arr): """ 숫자 야구 게임의 결과를 계산하는 함수 - 같은 위치에 같은 숫자가 있으면 스트라이크 - 다른 위치에 같은 숫자가 있으면 볼 """ - strikes=0 - balls=0 + strikes = 0 + balls = 0 for i in range(3):🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[warning] 38-38: E303 too many blank lines (3)
[warning] 44-44: E225 missing whitespace around operator
[warning] 45-45: E225 missing whitespace around operator
88-89
: 오류 처리 시 raise 문 유지swthewhite의 코멘트에 따라 raise 문이 올바르게 추가되었습니다. 이렇게 하면 예외가 상위 호출자에게 전파되어 프로그램이 종료됩니다.
하지만 사용자 경험을 위해 아래와 같은 대안도 고려해 볼 수 있습니다:
프로그램의 목적에 따라 오류 처리 방식을 선택할 수 있습니다:
- 현재 구현: 엄격한 검증 - 오류 발생 시 프로그램 종료
- 대안: 사용자 친화적 - 오류 발생 시 게임 재시작 질문으로 돌아가기
여러분의 요구사항에 따라 적절한 방식을 선택하세요.
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.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/baseball/main.py (1)
72-85
:⚠️ Potential issue메인 함수 로직에 오류가 있습니다.
메인 함수는 이전 리뷰에서 지적된 스타일 이슈들이 대부분 수정되었습니다. 하지만 코드 흐름에 문제가 있습니다.
84-85 라인에서 예외가 발생했을 때 게임이 종료되는데, 이전 리뷰 코멘트에 따르면 이는 의도된 동작이 아닙니다. 사용자가 잘못된 입력을 했을 때 게임을 종료하는 대신 다시 입력할 기회를 주어야 합니다.
또한 함수 설명 docstring을 추가하면 좋을 것 같습니다.
def main(): + """ + 프로그램의 메인 함수 + 게임 시작, 재시작, 종료 등 전체 프로그램 흐름을 관리합니다. + """ print("숫자 야구 게임을 시작합니다.") while True: try: game() choice = regame() if choice == 1: continue elif choice == 2: break except ValueError as e: print(e) - break + continue
🧹 Nitpick comments (5)
src/baseball/main.py (5)
4-17
: 입력 검증 로직이 잘 구현되었습니다.사용자 입력을 검증하는 로직이 체계적으로 구현되어 있습니다. 길이 확인, 숫자 여부 확인, 범위 확인(1-9), 중복 확인 등 필요한 검증 단계를 모두 포함하고 있습니다. 오류 메시지도 명확합니다.
다만, 함수 설명을 위한 docstring을 추가하면 좋을 것 같습니다.
def check_valid_input(): + """ + 사용자로부터 3자리 숫자를 입력받아 유효성을 검사하는 함수 + - 입력값이 3자리인지 확인 + - 각 자리가 1-9 사이의 숫자인지 확인 + - 중복된 숫자가 없는지 확인 + + Returns: + list: 검증된 3개의 정수로 이루어진 리스트 + Raises: + ValueError: 유효하지 않은 입력일 경우 + """ arr_list = list(input("숫자를 입력하세요: "))
20-25
: 재시작 함수가 개선되었습니다.이전 리뷰에서 지적된 스타일 이슈들이 모두 수정되었습니다. 연산자 주변의 공백, 쉼표 뒤의 공백, 함수 사이의 빈 줄 등이 적절히 적용되었습니다. 또한 입력 프롬프트 메시지도 추가되었습니다.
함수 설명 docstring을 추가하면 더 좋을 것 같습니다.
def regame(): + """ + 게임 재시작 여부를 사용자에게 묻는 함수 + + Returns: + int: 1(재시작) 또는 2(종료) + Raises: + ValueError: 유효하지 않은 입력일 경우 + """ val = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.: ")
28-29
: 난수 생성 함수가 간결하게 구현되었습니다.
random.sample()
을 활용하여 중복 없는 3개의 숫자를 효율적으로 생성하는 좋은 구현입니다. 함수 설명 docstring을 추가하면 좋을 것 같습니다.def make_num(): + """ + 1부터 9까지의 숫자 중 중복 없이 3개의 숫자를 무작위로 선택하는 함수 + + Returns: + list: 1-9 사이의 중복 없는 3개의 정수로 이루어진 리스트 + """ return random.sample(range(1, 10), 3)
32-33
: 간단한 래퍼 함수 구현이 함수는 단순히
check_valid_input()
을 호출하는 래퍼 함수입니다. 현재는 추가 기능이 없지만, 향후 확장성을 위한 것으로 보입니다. 함수 설명 docstring을 추가하면 좋을 것 같습니다.def input_num(): + """ + 사용자로부터 유효한 입력을 받아 반환하는 함수 + + Returns: + list: 검증된 3개의 정수로 이루어진 리스트 + Raises: + ValueError: 유효하지 않은 입력일 경우 + """ return check_valid_input()
55-69
: 게임 함수가 개선되었습니다.이전 리뷰에서 지적된 스타일 이슈들이 모두 수정되었습니다. 연산자 주변의 공백이 적절히 적용되었고, while 문이
while True
로 수정되어 더 파이썬스러워졌습니다.하지만 몇 가지 개선 사항을 제안합니다:
- 시도 횟수를 추적하여 사용자에게 보여주면 더 좋은 경험을 제공할 수 있습니다.
- 게임 종료 시 정답을 표시하면 사용자가 자신의 추측과 비교할 수 있습니다.
- 함수 설명 docstring을 추가하면 좋을 것 같습니다.
def game(): + """ + 숫자 야구 게임의 주요 로직을 담당하는 함수 + 컴퓨터가 생성한 3개의 숫자를 맞추는 게임을 진행합니다. + """ com = make_num() + attempts = 0 while True: + attempts += 1 + print(f"시도 #{attempts}") arr = input_num() strikes, balls = check(com, arr) if strikes == 0 and balls == 0: print("낫싱") elif strikes == 3: print("3스트라이크") - print("3개의 숫자를 모두 맞히셨습니다! 게임 종료") + print(f"3개의 숫자를 모두 맞히셨습니다! (시도 횟수: {attempts})") + print(f"정답은 {com} 이었습니다.") break else: print(f"{balls}볼 {strikes}스트라이크")
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/baseball/main.py
(1 hunks)
🔇 Additional comments (2)
src/baseball/main.py (2)
1-3
: 코드 구조가 개선되었습니다.전반적인 코드 구조가 깔끔하며, 필요한 라이브러리(random)를 적절히 가져오고, PEP8 스타일 가이드에 따라 빈 줄을 적절히 사용하고 있습니다.
36-52
: 비교 함수가 개선되었습니다.이전 리뷰에서 지적된 모든 스타일 이슈가 수정되었습니다. docstring이 추가되어 함수의 기능을 명확히 설명하고 있으며, 연산자 주변의 공백, 쉼표 뒤의 공백 등이 적절히 적용되었습니다.
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.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/baseball/main.py (1)
55-70
: 🛠️ Refactor suggestion게임 함수 개선이 필요합니다.
game()
함수의 스타일 이슈는 수정되었지만, 이전 리뷰에서 제안된 일부 개선 사항이 아직 적용되지 않았습니다:
- 시도 횟수 카운터가 추가되지 않았습니다.
- 사용자가 승리했을 때 정답(컴퓨터가 생성한 숫자)을 보여주지 않습니다.
이러한 기능은 게임 경험을 향상시킬 수 있습니다.
다음과 같이 개선해보세요:
def game(): com = make_num() + attempts = 0 while True: + attempts += 1 + print(f"시도 #{attempts}") arr = input_num() strikes, balls = check(com, arr) if strikes == 0 and balls == 0: print("낫싱") elif strikes == 3: print("3스트라이크") - print("3개의 숫자를 모두 맞히셨습니다! 게임 종료") + print(f"3개의 숫자를 모두 맞히셨습니다! (시도 횟수: {attempts})") + print(f"정답은 {com} 이었습니다.") break else: print(f"{balls}볼 {strikes}스트라이크")
🧹 Nitpick comments (2)
src/baseball/main.py (2)
32-33
: 불필요한 래퍼 함수
input_num()
함수는 단순히check_valid_input()
를 호출하는 래퍼 함수입니다. 이런 종류의 패스스루 래퍼 함수는 코드를 복잡하게 만들 수 있습니다. 직접check_valid_input()
을 호출하거나, 래퍼 함수를 유지하려면 추가적인 기능을 넣는 것이 좋겠습니다.
72-85
: 메인 함수의 예외 처리가 적절히 개선되었습니다.
main()
함수의 스타일 이슈가 수정되었으며, 예외 처리 방식이 개선되었습니다. 85번 줄에raise
문을 추가하여 예외를 상위로 전파하는 것은 적절합니다.다만, 예외가 발생하면 게임이 즉시 종료됩니다. 사용자 친화적인 측면에서는 다시 입력할 기회를 주는 것이 더 나을 수 있습니다.
다음과 같이 개선해보세요:
def main(): print("숫자 야구 게임을 시작합니다.") while True: try: game() choice = regame() if choice == 1: continue elif choice == 2: break except ValueError as e: print(e) - raise + continue
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/baseball/main.py
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/baseball/main.py
[warning] 11-11: src/baseball/main.py#L11
Added line #L11 was not covered by tests
[warning] 13-13: src/baseball/main.py#L13
Added line #L13 was not covered by tests
[warning] 15-15: src/baseball/main.py#L15
Added line #L15 was not covered by tests
[warning] 25-25: src/baseball/main.py#L25
Added line #L25 was not covered by tests
🔇 Additional comments (4)
src/baseball/main.py (4)
4-17
: 입력 검증 함수가 추가되었습니다만, 테스트 커버리지 개선이 필요합니다.
check_valid_input()
함수가 사용자 입력을 잘 검증하고 있습니다. 하지만 정적 분석에 따르면 일부 예외 처리 경로(11, 13, 15줄)가 테스트로 커버되지 않았습니다. 각 예외 상황에 대한 테스트 케이스를 추가하는 것이 좋겠습니다.또한 5번 줄 후의 빈 줄은 불필요하니 제거하는 것이 좋겠습니다.
테스트 커버리지를 개선하기 위해 다음과 같은 테스트 케이스를 추가하는 것을 고려해 보세요:
# 숫자가 아닌 문자 입력 테스트 # 범위를 벗어난 숫자 입력 테스트 (0이나 10 이상) # 중복된 숫자 입력 테스트🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 11-11: src/baseball/main.py#L11
Added line #L11 was not covered by tests
[warning] 13-13: src/baseball/main.py#L13
Added line #L13 was not covered by tests
[warning] 15-15: src/baseball/main.py#L15
Added line #L15 was not covered by tests
20-25
: 재시작 함수가 잘 구현되었습니다.
regame()
함수의 스타일 문제가 수정되었고, 입력 프롬프트 메시지도 추가되어 사용자 경험이 개선되었습니다.단, 이 함수의 예외 처리 경로(25줄)도 테스트로 커버되지 않은 것으로 보입니다. 테스트 케이스를 추가하는 것이 좋겠습니다.
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 25-25: src/baseball/main.py#L25
Added line #L25 was not covered by tests
28-29
: 난수 생성 함수가 적절히 구현되었습니다.
make_num()
함수가 1부터 9까지의 숫자 중 3개를 랜덤하게 선택하여 반환합니다.random.sample
을 사용해 중복 없는 숫자를 생성하는 것은 좋은 접근법입니다.
36-52
: 비교 함수가 잘 개선되었습니다.
check()
함수에 스타일 이슈가 수정되었고, 명확한 주석이 추가되어 가독성이 크게 향상되었습니다. 스트라이크와 볼 계산 로직도 정확하게 구현되어 있습니다.
Summary by CodeRabbit