-
Notifications
You must be signed in to change notification settings - Fork 5
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
[자동차 경주]소현우 과제 제출합니다. #1
base: main
Are you sure you want to change the base?
Changes from 3 commits
85d28df
f2a2b88
a252e0f
175d1a0
0e0930a
70e122e
a8f68ff
4ae58d8
468f74f
20341e2
c3928cf
c76e56f
d37e97a
e3ce741
ebd6725
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 | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,54 @@ | ||||||||||||||||||||||||||||||||||||||
""" 자동차 경주 게임 메인 스크립트 """ | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
import random | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def move_cars(car_positions): | ||||||||||||||||||||||||||||||||||||||
"""자동차 이동 로직 (랜덤 숫자가 4 이상이면 이동)""" | ||||||||||||||||||||||||||||||||||||||
for name in car_positions: | ||||||||||||||||||||||||||||||||||||||
if random.randint(1, 9) >= 4: | ||||||||||||||||||||||||||||||||||||||
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. 숫자상수는, 매직넘버상수로 바꾸고 전방선언 해주는 것이 좋습니다. |
||||||||||||||||||||||||||||||||||||||
car_positions[name] += 1 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def print_race(car_positions): | ||||||||||||||||||||||||||||||||||||||
"""현재 경주 상태 출력""" | ||||||||||||||||||||||||||||||||||||||
for name, pos in car_positions.items(): | ||||||||||||||||||||||||||||||||||||||
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. pos도 좋지만 |
||||||||||||||||||||||||||||||||||||||
if pos > 0: | ||||||||||||||||||||||||||||||||||||||
print(f"{name} : {'-' * pos}") | ||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||
print(f"{name} : {' '}") | ||||||||||||||||||||||||||||||||||||||
print() | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def get_winners(car_positions): | ||||||||||||||||||||||||||||||||||||||
"""최종 우승자 결정""" | ||||||||||||||||||||||||||||||||||||||
max_pos = max(car_positions.values()) | ||||||||||||||||||||||||||||||||||||||
return [name for name, pos in car_positions.items() if pos == max_pos] | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
def main(): | ||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||
프로그램의 진입점 함수. | ||||||||||||||||||||||||||||||||||||||
여기에서 전체 프로그램 로직을 시작합니다. | ||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||
# 프로그램의 메인 로직을 여기에 구현 | ||||||||||||||||||||||||||||||||||||||
print("프로그램이 시작되었습니다.") | ||||||||||||||||||||||||||||||||||||||
"""메인 함수""" | ||||||||||||||||||||||||||||||||||||||
car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분): ").split(",") | ||||||||||||||||||||||||||||||||||||||
car_names = list(dict.fromkeys(name.strip() for name in car_names if name.strip())) | ||||||||||||||||||||||||||||||||||||||
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. PEP8 스타일 가이드를 준수해야 합니다. 파이프라인 검사에서 발견된 스타일 문제를 수정해야 합니다. 다음 사항들을 수정해 주세요:
- car_names = list(dict.fromkeys(name.strip() for name in car_names if name.strip()))
+ car_names = list(dict.fromkeys(
+ name.strip() for name in car_names if name.strip()
+ ))
- N = int(input("시도할 횟수는 몇 회인가요? "))
+ num_attempts = int(input("시도할 횟수는 몇 회인가요? "))
- for _ in range(N):
+ for _ in range(num_attempts): Also applies to: 43-43, 52-52 🧰 Tools🪛 GitHub Actions: Check PEP8 Style[error] 36-36: line too long (87 > 79 characters) |
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
for name in car_names: | ||||||||||||||||||||||||||||||||||||||
if len(name) > 5: | ||||||||||||||||||||||||||||||||||||||
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. 숫자상수는, 매직넘버상수로 바꾸고 전방선언 해주는 것이 좋습니다. |
||||||||||||||||||||||||||||||||||||||
raise ValueError("⚠ 자동차 이름은 5자 이하만 가능합니다!") | ||||||||||||||||||||||||||||||||||||||
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 입력값 검증을 강화해야 합니다. 자동차 이름에 대한 추가 검증이 필요합니다:
car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분): ").split(",")
+ if not car_names:
+ raise ValueError("⚠ 자동차 이름을 입력해주세요!")
car_names = list(dict.fromkeys(name.strip() for name in car_names if name.strip()))
+ if not car_names:
+ raise ValueError("⚠ 유효한 자동차 이름이 없습니다!")
for name in car_names:
+ if not name.isalnum():
+ raise ValueError("⚠ 자동차 이름은 알파벳과 숫자만 가능합니다!")
if len(name) > 5:
raise ValueError("⚠ 자동차 이름은 5자 이하만 가능합니다!") 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: Check PEP8 Style[error] 29-29: E501 line too long (87 > 79 characters) |
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||
N = int(input("시도할 횟수는 몇 회인가요? ")) | ||||||||||||||||||||||||||||||||||||||
if N <= 0: | ||||||||||||||||||||||||||||||||||||||
raise ValueError("⚠ 시도 횟수는 1 이상이어야 합니다!") | ||||||||||||||||||||||||||||||||||||||
except ValueError: | ||||||||||||||||||||||||||||||||||||||
print("⚠ 잘못된 입력입니다. 숫자를 입력하세요.") | ||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||
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 예외 처리 로직을 개선해야 합니다. 시도 횟수 입력 처리에서 다음과 같은 문제가 있습니다:
다음과 같이 개선하는 것을 제안합니다: try:
N = int(input("시도할 횟수는 몇 회인가요? "))
- if N <= 0:
- raise ValueError("⚠ 시도 횟수는 1 이상이어야 합니다!")
- except ValueError:
- print("⚠ 잘못된 입력입니다. 숫자를 입력하세요.")
- return
+ if N < 1:
+ raise ValueError("⚠ 시도 횟수는 1 이상이어야 합니다!")
+ except ValueError as e:
+ raise ValueError("⚠ 잘못된 입력입니다. 1 이상의 숫자를 입력하세요.") 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
car_positions = {name: 0 for name in car_names} # 자동차 위치 초기화 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
for _ in range(N): | ||||||||||||||||||||||||||||||||||||||
move_cars(car_positions) # 자동차 이동 | ||||||||||||||||||||||||||||||||||||||
print_race(car_positions) # 현재 상태 출력 | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
winners = get_winners(car_positions) # 우승자 결정 | ||||||||||||||||||||||||||||||||||||||
print(f"\n최종 우승자 : {', '.join(winners)}") | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||
# 프로그램이 직접 실행될 때만 main() 함수를 호출 | ||||||||||||||||||||||||||||||||||||||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ def test_전진_및_정지(capsys): | |
main() # 프로그램 실행 | ||
|
||
# 출력값을 캡처한 후 검증 | ||
캡처된_출력 = capsys.readouterr() | ||
캡처된_출력 = capsys.readouterr().out | ||
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. ㅎㅎ 제가 실수한 부분인데 잘 체크하고 고치셨어요! |
||
assert all(예상_출력 in 캡처된_출력 for 예상_출력 in ["pobi : -", "woni : ", "최종 우승자 : pobi"]) | ||
|
||
|
||
|
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.
PEP8 스타일 가이드를 준수해야 합니다.
다음과 같은 스타일 문제를 수정해 주세요:
N
은 snake_case 명명 규칙을 따라야 합니다Also applies to: 11-11, 26-26, 29-29, 36-36, 45-45
🧰 Tools
🪛 GitHub Actions: Check PEP8 Style
[error] 5-5: E302 expected 2 blank lines, found 1