-
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 1 commit
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,41 @@ | ||||||||||||||||||||||||||||
def main(): | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
프로그램의 진입점 함수. | ||||||||||||||||||||||||||||
여기에서 전체 프로그램 로직을 시작합니다. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
# 프로그램의 메인 로직을 여기에 구현 | ||||||||||||||||||||||||||||
print("프로그램이 시작되었습니다.") | ||||||||||||||||||||||||||||
import random | ||||||||||||||||||||||||||||
car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분): ").split(",") | ||||||||||||||||||||||||||||
car_names = list(set(name.strip() for name in car_names if name.strip())) | ||||||||||||||||||||||||||||
car_dict = {} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
for name in car_names: | ||||||||||||||||||||||||||||
name = name.strip() | ||||||||||||||||||||||||||||
if len(name) > 5: | ||||||||||||||||||||||||||||
raise ValueError("⚠ 자동차 이름은 5자 이하만 가능합니다!") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
car_dict[name] = 0 | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
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
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
carPos = {name: 0 for name in car_dict} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
for _ in range(N): | ||||||||||||||||||||||||||||
for name in car_dict: | ||||||||||||||||||||||||||||
rand_num = random.randint(1, 9) | ||||||||||||||||||||||||||||
if rand_num >= 4: | ||||||||||||||||||||||||||||
carPos[name] += 1 | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
for name in carPos: | ||||||||||||||||||||||||||||
print(f"{name} : {'-' * carPos[name] if carPos[name] > 0 else ' '}") | ||||||||||||||||||||||||||||
print() # 줄바꿈 추가 | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print() # 최종 출력과 구분을 위해 개행 추가 | ||||||||||||||||||||||||||||
maxPos = max(carPos.values()) | ||||||||||||||||||||||||||||
winners = [name for name, pos in carPos.items() if pos == maxPos] | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print(f"최종 우승자 : {', '.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.
숫자상수는, 매직넘버상수로 바꾸고 전방선언 해주는 것이 좋습니다.