From a40b73b3d7dbe9dd8cb3b7ab15cd0383c1abce2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:28:11 +0900 Subject: [PATCH 01/23] =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 29 +++++++++++++++++++++++++++++ src/racingcar/main.py | 12 ------------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/docs/README.md b/docs/README.md index e69de29..c944496 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,29 @@ +# 자동차 경주 게임 + +## 기능 목록 + +1. **자동차 이름 입력** + - 사용자가가 쉼표(`,`)로 구분된 자동차 이름을 입력 + - 각 자동차 이름은 1~5자의 문자 + - 잘못된 입력일 경우 `ValueError`를 발생시키고 프로그램을 종료료 + +2. **이동 횟수 입력** + - 사용자가 몇 번의 이동을 할 것인지 숫자로 입력 + - 1 이상의 정수를 입력해야 하며, 잘못된 입력 시 `ValueError` 발생 + +3. **자동차 이동 로직** + - 각 자동차는 매 턴마다 0~9 사이의 무작위 값을 얻음 + - 값이 4 이상이면 자동차는 한 칸(`-`) 전진 + - 값이 3 이하이면 그대로 멈춤 + +4. **경기 진행 및 결과 출력** + - 입력받은 횟수만큼 경기를 진행 + - 매 턴마다 각 자동차의 진행 상태를 출력 + +5. **우승자 선정** + - 가장 멀리 간 자동차가 우승자 + - 우승자가 여러 명일 경우 쉼표(`,`)로 구분하여 출력 + +6. **예외 처리** + - 잘못된 자동차 이름(1~5자 초과 또는 공백) 입력 시 `ValueError` 발생 + - 이동 횟수를 1 이상의 정수로 입력하지 않으면 `ValueError` 발생 \ No newline at end of file diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 4128547..e69de29 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,12 +0,0 @@ -def main(): - """ - 프로그램의 진입점 함수. - 여기에서 전체 프로그램 로직을 시작합니다. - """ - # 프로그램의 메인 로직을 여기에 구현 - print("프로그램이 시작되었습니다.") - - -if __name__ == "__main__": - # 프로그램이 직접 실행될 때만 main() 함수를 호출 - main() From c069935cbe35d9d37bae285f7198a60f1def0f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:29:13 +0900 Subject: [PATCH 02/23] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index e69de29..80e7a09 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -0,0 +1,8 @@ +def get_car_names(): + names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n").split(",") + names = [name.strip() for name in names] + + if not names or any(len(name) > 5 or not name for name in names): + raise ValueError("자동차 이름은 1~5자의 문자여야 합니다.") + + return names \ No newline at end of file From 0b7e82a63026a80427bd312156c353865edbeac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:30:12 +0900 Subject: [PATCH 03/23] =?UTF-8?q?=EC=9D=B4=EB=8F=99=20=ED=9A=9F=EC=88=98?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 80e7a09..16c69d8 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -5,4 +5,13 @@ def get_car_names(): if not names or any(len(name) > 5 or not name for name in names): raise ValueError("자동차 이름은 1~5자의 문자여야 합니다.") - return names \ No newline at end of file + return names + +def get_attempt_count(): + try: + count = int(input("시도할 횟수는 몇 회인가요?\n")) + if count <= 0: + raise ValueError + return count + except ValueError: + raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") \ No newline at end of file From 4b579ff62550b46dc27116bf7ee1dd2a8bcb533b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:31:53 +0900 Subject: [PATCH 04/23] =?UTF-8?q?=EC=9E=90=EB=8F=99=EC=B0=A8=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 16c69d8..3eca738 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,3 +1,5 @@ +import random + def get_car_names(): names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n").split(",") names = [name.strip() for name in names] @@ -14,4 +16,7 @@ def get_attempt_count(): raise ValueError return count except ValueError: - raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") \ No newline at end of file + raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") + +def move_car(): + return "-" if random.randint(0, 9) >= 4 else "" \ No newline at end of file From 75982a7d1af3e77e463c1d0702d86bc093e1deb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:32:27 +0900 Subject: [PATCH 05/23] =?UTF-8?q?=EA=B2=BD=EA=B8=B0=20=EC=A7=84=ED=96=89?= =?UTF-8?q?=20=EB=B0=8F=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 3eca738..6e12d45 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -19,4 +19,16 @@ def get_attempt_count(): raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") def move_car(): - return "-" if random.randint(0, 9) >= 4 else "" \ No newline at end of file + return "-" if random.randint(0, 9) >= 4 else "" + +def run_race(cars, attempts): + results = {car: "" for car in cars} + + print("\n실행 결과") + for _ in range(attempts): + for car in cars: + results[car] += move_car() + for car, progress in results.items(): + print(f"{car} : {progress}") + print() + return results \ No newline at end of file From c4b2b41f277c619b85bcaba1f11571918fdc487a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:32:55 +0900 Subject: [PATCH 06/23] =?UTF-8?q?=EC=9A=B0=EC=8A=B9=EC=9E=90=20=EC=84=A0?= =?UTF-8?q?=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 6e12d45..ef06319 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -31,4 +31,9 @@ def run_race(cars, attempts): for car, progress in results.items(): print(f"{car} : {progress}") print() - return results \ No newline at end of file + return results + +def get_winners(results): + max_distance = max(len(progress) for progress in results.values()) + winners = [car for car, progress in results.items() if len(progress) == max_distance] + return winners \ No newline at end of file From 0a6a231d33acca6e99312d4b1c2da0a8ddf7119b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:34:34 +0900 Subject: [PATCH 07/23] =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20=EB=B0=8F=20main()=20=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index ef06319..bd54eb7 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -36,4 +36,18 @@ def run_race(cars, attempts): def get_winners(results): max_distance = max(len(progress) for progress in results.values()) winners = [car for car, progress in results.items() if len(progress) == max_distance] - return winners \ No newline at end of file + return winners + +def main(): + try: + cars = get_car_names() + attempts = get_attempt_count() + results = run_race(cars, attempts) + winners = get_winners(results) + print(f"최종 우승자 : {', '.join(winners)}") + except ValueError as e: + print(f"입력 오류: {e}") + raise + +if __name__ == "__main__": + main() From 80eee98c7a41716ee15b56e063cb4636cdae1918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:42:35 +0900 Subject: [PATCH 08/23] =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=ED=86=B5?= =?UTF-8?q?=EA=B3=BC=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index bd54eb7..c371271 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -15,11 +15,13 @@ def get_attempt_count(): if count <= 0: raise ValueError return count - except ValueError: - raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") + except ValueError as e: + raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") from e def move_car(): - return "-" if random.randint(0, 9) >= 4 else "" + if random.randint(0, 9) >= 4: + return "-" + return "" def run_race(cars, attempts): results = {car: "" for car in cars} @@ -35,7 +37,10 @@ def run_race(cars, attempts): def get_winners(results): max_distance = max(len(progress) for progress in results.values()) - winners = [car for car, progress in results.items() if len(progress) == max_distance] + winners = [ + car for car, progress in results.items() + if len(progress) == max_distance + ] return winners def main(): From 20bed73c33e2e9e6a4a97bd6b108528d9e27cfb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:45:53 +0900 Subject: [PATCH 09/23] =?UTF-8?q?=EC=B5=9C=EC=A2=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index c944496..5417fd1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,7 +5,7 @@ 1. **자동차 이름 입력** - 사용자가가 쉼표(`,`)로 구분된 자동차 이름을 입력 - 각 자동차 이름은 1~5자의 문자 - - 잘못된 입력일 경우 `ValueError`를 발생시키고 프로그램을 종료료 + - 잘못된 입력일 경우 `ValueError`를 발생시키고 프로그램을 종료 2. **이동 횟수 입력** - 사용자가 몇 번의 이동을 할 것인지 숫자로 입력 From 2e4f3e51f8afb1ffa5bba6452989cb7096ef34b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:50:51 +0900 Subject: [PATCH 10/23] =?UTF-8?q?=EB=8B=A4=EC=8B=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index c371271..316d09a 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,5 +1,7 @@ import random + + def get_car_names(): names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n").split(",") names = [name.strip() for name in names] @@ -9,6 +11,8 @@ def get_car_names(): return names + + def get_attempt_count(): try: count = int(input("시도할 횟수는 몇 회인가요?\n")) @@ -18,11 +22,15 @@ def get_attempt_count(): except ValueError as e: raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") from e + + def move_car(): if random.randint(0, 9) >= 4: return "-" return "" + + def run_race(cars, attempts): results = {car: "" for car in cars} @@ -35,6 +43,8 @@ def run_race(cars, attempts): print() return results + + def get_winners(results): max_distance = max(len(progress) for progress in results.values()) winners = [ @@ -43,6 +53,8 @@ def get_winners(results): ] return winners + + def main(): try: cars = get_car_names() @@ -52,7 +64,9 @@ def main(): print(f"최종 우승자 : {', '.join(winners)}") except ValueError as e: print(f"입력 오류: {e}") - raise + raise + + if __name__ == "__main__": main() From 71066bae8be18e0c1237c818797edf3d5db022cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:54:29 +0900 Subject: [PATCH 11/23] =?UTF-8?q?raise=EB=B6=80=EB=B6=84=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 316d09a..d74e837 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -68,5 +68,6 @@ def main(): + if __name__ == "__main__": main() From e0aa6fbb887f1c093522332d5c06db789613d0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 21:56:07 +0900 Subject: [PATCH 12/23] =?UTF-8?q?=EA=B3=B5=EB=B0=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index d74e837..8be0cb3 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,7 +1,6 @@ import random - def get_car_names(): names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n").split(",") names = [name.strip() for name in names] From c323efc47e58aeb1c50eb49be116deea4e6e6ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 22:00:05 +0900 Subject: [PATCH 13/23] =?UTF-8?q?PEP8=20=EC=8A=A4=ED=83=80=EC=9D=BC?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EC=B6=B0=20=EB=8B=A4=EC=8B=9C=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 8be0cb3..1a6f697 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -2,7 +2,9 @@ def get_car_names(): - names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n").split(",") + names = input( + "경주할 자동차 이름을 입력하세요. (이름은 쉼표로 구분)\n" + ).split(",") names = [name.strip() for name in names] if not names or any(len(name) > 5 or not name for name in names): @@ -11,23 +13,18 @@ def get_car_names(): return names - def get_attempt_count(): try: count = int(input("시도할 횟수는 몇 회인가요?\n")) if count <= 0: raise ValueError return count - except ValueError as e: - raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") from e - + except ValueError: + raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") def move_car(): - if random.randint(0, 9) >= 4: - return "-" - return "" - + return "-" if random.randint(0, 9) >= 4 else "" def run_race(cars, attempts): @@ -37,21 +34,21 @@ def run_race(cars, attempts): for _ in range(attempts): for car in cars: results[car] += move_car() + for car, progress in results.items(): print(f"{car} : {progress}") + print() - return results + return results def get_winners(results): max_distance = max(len(progress) for progress in results.values()) - winners = [ + return [ car for car, progress in results.items() if len(progress) == max_distance - ] - return winners - + ] def main(): @@ -60,12 +57,12 @@ def main(): attempts = get_attempt_count() results = run_race(cars, attempts) winners = get_winners(results) - print(f"최종 우승자 : {', '.join(winners)}") - except ValueError as e: - print(f"입력 오류: {e}") - raise + print(f"최종 우승자 : {', '.join(winners)}") + except ValueError as error: + print(f"입력 오류: {error}") + raise if __name__ == "__main__": From f1b92eaa89fde43a4fcf0adf97e6d29a008ad1e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Mon, 10 Feb 2025 22:02:25 +0900 Subject: [PATCH 14/23] =?UTF-8?q?=EC=82=BC=ED=95=AD=EC=97=B0=EC=82=B0?= =?UTF-8?q?=EC=9E=90=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 1a6f697..a8c758a 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -24,7 +24,9 @@ def get_attempt_count(): def move_car(): - return "-" if random.randint(0, 9) >= 4 else "" + if random.randint(0, 9) >= 4: + return "-" + return "" def run_race(cars, attempts): From 4ab0a42ec649cdd7bb1a39dfa07ed152b88b06a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 17:21:25 +0900 Subject: [PATCH 15/23] =?UTF-8?q?=EC=88=AB=EC=9E=90=EC=83=81=EC=88=98=20?= =?UTF-8?q?=EB=A7=A4=EC=A7=81=EB=84=98=EB=B2=84=EC=83=81=EC=88=98=EB=A1=9C?= =?UTF-8?q?=20=EC=A0=84=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index a8c758a..2c1fb0c 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,5 +1,7 @@ import random +THRESHOLD = 4 # 자동차가 이동할 확률을 결정하는 기준값 +RANDOM_MAX = 9 def get_car_names(): names = input( @@ -24,7 +26,7 @@ def get_attempt_count(): def move_car(): - if random.randint(0, 9) >= 4: + if random.randint(0, RANDOM_MAX) >= THRESHOLD: return "-" return "" From f7d45cea765ae03063a098e95a56d8722f21e7ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 17:30:36 +0900 Subject: [PATCH 16/23] =?UTF-8?q?fix=20:=20=EC=83=81=EC=88=98=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 2c1fb0c..623aeed 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,8 +1,9 @@ import random -THRESHOLD = 4 # 자동차가 이동할 확률을 결정하는 기준값 +THRESHOLD = 4 RANDOM_MAX = 9 + def get_car_names(): names = input( "경주할 자동차 이름을 입력하세요. (이름은 쉼표로 구분)\n" From 6837570834774638d0b114a1605a4cfbac70a3b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 17:36:53 +0900 Subject: [PATCH 17/23] =?UTF-8?q?fix:=20PEP8=EC=97=90=20=EB=A7=9E=EC=B6=B0?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 623aeed..44a3409 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,13 +1,10 @@ import random -THRESHOLD = 4 +THRESHOLD = 4 RANDOM_MAX = 9 - def get_car_names(): - names = input( - "경주할 자동차 이름을 입력하세요. (이름은 쉼표로 구분)\n" - ).split(",") + names = input("경주할 자동차 이름을 입력하세요. (이름은 쉼표로 구분)\n").split(",") names = [name.strip() for name in names] if not names or any(len(name) > 5 or not name for name in names): @@ -15,22 +12,17 @@ def get_car_names(): return names - def get_attempt_count(): try: count = int(input("시도할 횟수는 몇 회인가요?\n")) if count <= 0: - raise ValueError + raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") return count - except ValueError: - raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") - + except ValueError as error: + raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") from error def move_car(): - if random.randint(0, RANDOM_MAX) >= THRESHOLD: - return "-" - return "" - + return "-" if random.randint(0, RANDOM_MAX) >= THRESHOLD else "" def run_race(cars, attempts): results = {car: "" for car in cars} @@ -42,19 +34,13 @@ def run_race(cars, attempts): for car, progress in results.items(): print(f"{car} : {progress}") - print() - + return results - def get_winners(results): max_distance = max(len(progress) for progress in results.values()) - return [ - car for car, progress in results.items() - if len(progress) == max_distance - ] - + return [car for car, progress in results.items() if len(progress) == max_distance] def main(): try: @@ -62,13 +48,10 @@ def main(): attempts = get_attempt_count() results = run_race(cars, attempts) winners = get_winners(results) - print(f"최종 우승자 : {', '.join(winners)}") - except ValueError as error: print(f"입력 오류: {error}") raise - if __name__ == "__main__": - main() + main() \ No newline at end of file From 22e727ef11f5dbeed29bf0b69b1221a85bccd425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 17:52:54 +0900 Subject: [PATCH 18/23] =?UTF-8?q?fix:=20test=EC=8B=A4=ED=8C=A8=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 44a3409..f162b0d 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -3,6 +3,7 @@ THRESHOLD = 4 RANDOM_MAX = 9 + def get_car_names(): names = input("경주할 자동차 이름을 입력하세요. (이름은 쉼표로 구분)\n").split(",") names = [name.strip() for name in names] @@ -48,10 +49,13 @@ def main(): attempts = get_attempt_count() results = run_race(cars, attempts) winners = get_winners(results) + print(f"최종 우승자 : {', '.join(winners)}") + except ValueError as error: print(f"입력 오류: {error}") raise + if __name__ == "__main__": - main() \ No newline at end of file + main() From 4ef52ec7b8b37f015112234edcea8d707fa68d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 18:00:45 +0900 Subject: [PATCH 19/23] =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index f162b0d..ed3411b 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,6 +1,8 @@ import random +# 자동차가 전진하는 기준값 THRESHOLD = 4 +# 난수 생성 범위의 최대값 RANDOM_MAX = 9 @@ -23,7 +25,9 @@ def get_attempt_count(): raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") from error def move_car(): - return "-" if random.randint(0, RANDOM_MAX) >= THRESHOLD else "" + if random.randint(0, RANDOM_MAX) >= THRESHOLD: + return "-" + return "" def run_race(cars, attempts): results = {car: "" for car in cars} @@ -41,7 +45,10 @@ def run_race(cars, attempts): def get_winners(results): max_distance = max(len(progress) for progress in results.values()) - return [car for car, progress in results.items() if len(progress) == max_distance] + return [ + car for car, progress in results.items() + if len(progress) == max_distance + ] def main(): try: From a660e9f511be9d6fdc1bc5f931877974cf6d11eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 18:05:33 +0900 Subject: [PATCH 20/23] 1 --- src/racingcar/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index ed3411b..9bce1fc 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -5,7 +5,6 @@ # 난수 생성 범위의 최대값 RANDOM_MAX = 9 - def get_car_names(): names = input("경주할 자동차 이름을 입력하세요. (이름은 쉼표로 구분)\n").split(",") names = [name.strip() for name in names] @@ -40,13 +39,12 @@ def run_race(cars, attempts): for car, progress in results.items(): print(f"{car} : {progress}") print() - return results def get_winners(results): max_distance = max(len(progress) for progress in results.values()) return [ - car for car, progress in results.items() + car for car, progress in results.items() if len(progress) == max_distance ] From 0b74e461dfbb695c83d85d683c4a39a3ec60132a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 18:12:07 +0900 Subject: [PATCH 21/23] =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 9bce1fc..804dc30 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -14,6 +14,7 @@ def get_car_names(): return names + def get_attempt_count(): try: count = int(input("시도할 횟수는 몇 회인가요?\n")) @@ -23,11 +24,13 @@ def get_attempt_count(): except ValueError as error: raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") from error + def move_car(): if random.randint(0, RANDOM_MAX) >= THRESHOLD: return "-" return "" + def run_race(cars, attempts): results = {car: "" for car in cars} @@ -41,6 +44,7 @@ def run_race(cars, attempts): print() return results + def get_winners(results): max_distance = max(len(progress) for progress in results.values()) return [ @@ -48,6 +52,7 @@ def get_winners(results): if len(progress) == max_distance ] + def main(): try: cars = get_car_names() @@ -56,11 +61,11 @@ def main(): winners = get_winners(results) print(f"최종 우승자 : {', '.join(winners)}") - + except ValueError as error: print(f"입력 오류: {error}") raise - + if __name__ == "__main__": main() From a99e7f68e9c91aa168d0e74aaa253b7c976ab414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 23:13:54 +0900 Subject: [PATCH 22/23] =?UTF-8?q?fix:=208=EB=B2=88=EC=A7=B8=20=EC=A4=84=20?= =?UTF-8?q?=EC=A4=84=EB=B0=94=EA=BF=88=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 804dc30..b18976d 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -5,6 +5,7 @@ # 난수 생성 범위의 최대값 RANDOM_MAX = 9 + def get_car_names(): names = input("경주할 자동차 이름을 입력하세요. (이름은 쉼표로 구분)\n").split(",") names = [name.strip() for name in names] @@ -54,17 +55,19 @@ def get_winners(results): def main(): - try: - cars = get_car_names() - attempts = get_attempt_count() - results = run_race(cars, attempts) - winners = get_winners(results) - - print(f"최종 우승자 : {', '.join(winners)}") - - except ValueError as error: - print(f"입력 오류: {error}") - raise + while True: + try: + cars = get_car_names() + attempts = get_attempt_count() + results = run_race(cars, attempts) + winners = get_winners(results) + print(f"최종 우승자 : {', '.join(winners)}") + break + except ValueError as error: + print(f"입력 오류: {error}") + retry = input("다시 시도하시겠습니까? (y/n): ") + if retry.lower() != 'y': + break if __name__ == "__main__": From eba3e991cae34555a7fbf942d615e70a9cb70aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B7=9C?= Date: Thu, 13 Feb 2025 23:21:02 +0900 Subject: [PATCH 23/23] =?UTF-8?q?fix:=20pytest=20=EB=8F=99=EA=B3=BC?= =?UTF-8?q?=ED=95=98=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index b18976d..24101ee 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -20,9 +20,11 @@ def get_attempt_count(): try: count = int(input("시도할 횟수는 몇 회인가요?\n")) if count <= 0: - raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") + raise ValueError("시도 횟수는 양의 정수여야 합니다.") return count except ValueError as error: + if str(error) == "시도 횟수는 양의 정수여야 합니다.": + raise raise ValueError("올바른 횟수를 입력하세요. (양의 정수)") from error @@ -55,19 +57,15 @@ def get_winners(results): def main(): - while True: - try: - cars = get_car_names() - attempts = get_attempt_count() - results = run_race(cars, attempts) - winners = get_winners(results) - print(f"최종 우승자 : {', '.join(winners)}") - break - except ValueError as error: - print(f"입력 오류: {error}") - retry = input("다시 시도하시겠습니까? (y/n): ") - if retry.lower() != 'y': - break + try: + cars = get_car_names() + attempts = get_attempt_count() + results = run_race(cars, attempts) + winners = get_winners(results) + print(f"최종 우승자 : {', '.join(winners)}") + except ValueError as error: + print(f"입력 오류: {error}") + raise if __name__ == "__main__":