-
Notifications
You must be signed in to change notification settings - Fork 10
[숫자 야구 게임] 김지성 미션 제출합니다. #4
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 3 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,16 @@ | ||
| 입력 기능 | ||
| -서로 다른 3자리 수 | ||
| -게임이 끝난 후 1,2 입력 | ||
|
|
||
|
|
||
| 출력 기능 | ||
| -게임 시작문구 "숫자 야구 게임을 시작합니다." 출력 | ||
| -입력한 수에 대한 결과 출력 | ||
| -볼, 스트라이크 출력 | ||
| -아무 것도 없을 시 "낫싱"출력 | ||
| -일치 시 "3개의 숫자를 모두 맞히셨습니다! 게임 종료" 출력 | ||
| -"게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."출력 | ||
|
|
||
| 예외 처리 및 종료 기능능 | ||
| -잘못된 값 입력 시 ValueError 발생, 프로그램 종료 | ||
| - | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,87 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import random | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def randomnumber(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return random.sample(range(1, 10), 3) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def nothing(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return print("낫싱") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def onlystrike(x): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return print("%d스트라이크"%x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def onlyball(x): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return print("%d볼"%x) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def ballandstrike(x,y) : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return print("%d볼 %d스트라이크"%(x,y)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def gamestart(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return print("숫자 야구 게임을 시작합니다") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def pick(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return print("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def main(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 프로그램의 진입점 함수. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 여기에서 전체 프로그램 로직을 시작합니다. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| print('숫자 야구 게임을 시작합니다') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| strike=0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ball=0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| l=[] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| computer=randomnumber() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
1
to
+30
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 코드 개선이 필요한 부분이 있습니다.
다음과 같이 수정하는 것을 제안합니다: def main():
- print('숫자 야구 게임을 시작합니다')
+ gamestart()
while 1:
strike = 0
ball = 0
- l = []
+ user_numbers = []
computer = randomnumber()📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.8.2)30-30: Ambiguous variable name: (E741) |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| while 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| computer1=computer.copy() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| strike=0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ball=0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| l=[] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| a=input('숫자를 입력해주세요 :') | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(a) !=3 : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for r in range(len(a)) : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| l.append(int(a[r])) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+43
to
+46
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. 입력 값 검증이 불충분합니다. 현재는 길이만 확인하고 있습니다. 다음 사항들도 검증해야 합니다:
다음과 같은 개선된 구현을 제안합니다: a = input('숫자를 입력해주세요 :')
if len(a) != 3:
- raise ValueError
+ raise ValueError("3자리 숫자를 입력해주세요")
+
+ if not a.isdigit():
+ raise ValueError("숫자만 입력 가능합니다")
+ numbers = [int(x) for x in a]
+ if len(set(numbers)) != 3:
+ raise ValueError("중복되지 않은 숫자를 입력해주세요")
+
+ if not all(1 <= x <= 9 for x in numbers):
+ raise ValueError("1부터 9까지의 숫자만 입력 가능합니다")
+
- for r in range(len(a)):
- l.append(int(a[r]))
+ user_numbers = numbers📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in range(len(computer)-1,-1,-1) : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if computer[i] == l[i] : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| strike+=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| computer.pop(i) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| l.pop(i) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for k in range(len(l)) : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if l[k] in computer : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ball+=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| computer=computer1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 게임 로직을 더 효율적으로 개선할 수 있습니다. 현재 구현은 불필요한 복사 연산과 복잡한 로직을 포함하고 있습니다. 다음과 같은 개선된 구현을 제안합니다: - for i in range(len(computer)-1,-1,-1):
- if computer[i] == l[i]:
- strike += 1
- computer.pop(i)
- l.pop(i)
-
- for k in range(len(l)):
- if l[k] in computer:
- ball += 1
-
- computer = computer1
+ # 스트라이크 계산
+ strike = sum(1 for i in range(3) if user_numbers[i] == computer[i])
+
+ # 볼 계산
+ ball = sum(1 for i in range(3) for j in range(3)
+ if i != j and user_numbers[i] == computer[j])이렇게 하면:
📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if ball==0 and strike ==0 : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| nothing() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elif strike==3 : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onlystrike(strike) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| print('게임 종료') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| elif ball==0 : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onlyball(ball) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elif strike==0 : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| onlystrike(strike) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| elif ball!=0 and strike!=0 : | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ballandstrike(ball,strike) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| press=input() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if press!='1': | ||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 게임 재시작 로직이 일관성이 없습니다.
다음과 같이 수정하는 것을 제안합니다: - press = input()
- if press != '1':
+ pick()
+ press = input().strip()
+ if press not in ['1', '2']:
+ raise ValueError("1 또는 2만 입력 가능합니다")
+ if press == '2':
break📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # 프로그램의 메인 로직을 여기에 구현 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
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
README.md 파일의 개선이 필요합니다.
다음과 같은 문제점들이 있습니다:
다음과 같이 수정하는 것을 제안합니다:
📝 Committable suggestion