From ab80647b6acf7eef549e2e95191b3f2ea62541fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=98=EB=82=98?= Date: Sat, 8 Feb 2025 01:46:41 +0900 Subject: [PATCH 01/18] =?UTF-8?q?feat:=20=EA=B8=B0=EB=8A=A5=20=EC=84=A4?= =?UTF-8?q?=EA=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/README.md b/docs/README.md index e69de29..2b5e7fe 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,13 @@ +<조건> +>들여쓰기 깊이 2까지만 허용 +>함수나 메서드가 한 가지 일만 하도록 최대한 작게 만들기 + +<기본 입출력> +>자동차 이름과 시도 횟수 입력받기 +>실행 결과 (전진 횟수 * '-' 사용) +>최종 우승자 + +<전진 여부 함수> +>0~9 사이 무작위 값 +>그 값이 4 이상이면 전진 + From 0a0f230ea00bde5298738a1917f8bdf6accb067e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=98=EB=82=98?= Date: Sat, 8 Feb 2025 01:52:47 +0900 Subject: [PATCH 02/18] =?UTF-8?q?feat:=20=EA=B8=B0=EB=B3=B8=20=EC=9E=85?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 4128547..31c6f39 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,11 +1,15 @@ def main(): - """ - 프로그램의 진입점 함수. - 여기에서 전체 프로그램 로직을 시작합니다. - """ - # 프로그램의 메인 로직을 여기에 구현 print("프로그램이 시작되었습니다.") + car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") + cars = car_names.split(",") + + print(cars) + + attempts = input("시도할 횟수는 몇 회인가요?\n") + + print("실행 결과\n") + if __name__ == "__main__": # 프로그램이 직접 실행될 때만 main() 함수를 호출 From 120d6899b4e4669d5c36f876cafc04405284955d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=98=EB=82=98?= Date: Sat, 8 Feb 2025 02:13:00 +0900 Subject: [PATCH 03/18] =?UTF-8?q?feat:=20=EC=A0=84=EC=A7=84=20=EC=97=AC?= =?UTF-8?q?=EB=B6=80=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 | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 31c6f39..2dd6ba6 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,16 +1,23 @@ +import random def main(): print("프로그램이 시작되었습니다.") car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") cars = car_names.split(",") + car_positions = {car : 0 for car in cars} print(cars) + print(car_positions) - attempts = input("시도할 횟수는 몇 회인가요?\n") + attempts = int(input("시도할 횟수는 몇 회인가요?\n")) - print("실행 결과\n") + print("\n실행 결과") + +def movement(cars, car_positions) : + for car in cars : + if (random.randint(0,9) >= 4) : + car_positions[car] += 1 if __name__ == "__main__": - # 프로그램이 직접 실행될 때만 main() 함수를 호출 main() From f1efa11201ccfded317b1bb06d9f90a6eb1134dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=98=EB=82=98?= Date: Sat, 8 Feb 2025 02:25:40 +0900 Subject: [PATCH 04/18] =?UTF-8?q?feat:=20=EC=B5=9C=EC=A2=85=20=EC=9A=B0?= =?UTF-8?q?=EC=8A=B9=EC=9E=90=20=EC=B6=9C=EB=A0=A5=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=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 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 2dd6ba6..37f1b0e 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -6,18 +6,31 @@ def main(): cars = car_names.split(",") car_positions = {car : 0 for car in cars} - print(cars) - print(car_positions) - attempts = int(input("시도할 횟수는 몇 회인가요?\n")) print("\n실행 결과") + for i in range(attempts) : + movement(cars, car_positions) + for car in cars : + print(car + " : " + '-' * car_positions[car]) + print() + printWinner(cars, car_positions) def movement(cars, car_positions) : for car in cars : if (random.randint(0,9) >= 4) : car_positions[car] += 1 +def printWinner(cars, car_positions) : + winners = [] + max_value = max(car_positions.values()) + + for car in cars : + if (car_positions[car] == max_value) : + winners.append(car) + + print(f"최종 우승자 : {', '.join(winners)}") + if __name__ == "__main__": main() From da1723a7ef8e4538ae18a796057c7fb654404c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=98=EB=82=98?= Date: Sat, 8 Feb 2025 02:47:05 +0900 Subject: [PATCH 05/18] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 37f1b0e..d85b3b7 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -4,9 +4,16 @@ def main(): car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") cars = car_names.split(",") + for car in cars : + if (len(car) > 5) : + raise ValueError("이름이 5자를 초과합니다.") + car_positions = {car : 0 for car in cars} - attempts = int(input("시도할 횟수는 몇 회인가요?\n")) + attempts = input("시도할 횟수는 몇 회인가요?\n") + if (not attempts.isdigit()) : + raise ValueError("시도 횟수는 숫자로 입력하세요.") + attempts = int(attempts) print("\n실행 결과") for i in range(attempts) : From 9f466296234aa96c3d465b7f30586770186b2b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=98=EB=82=98?= Date: Sat, 8 Feb 2025 02:49:02 +0900 Subject: [PATCH 06/18] =?UTF-8?q?docs:=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EB=82=B4=EC=9A=A9=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 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 2b5e7fe..9ff93b7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,13 +1,18 @@ <조건> ->들여쓰기 깊이 2까지만 허용 +>들여쓰기 깊이 2까지만 허용 >함수나 메서드가 한 가지 일만 하도록 최대한 작게 만들기 +>입력이 올바르지 않은 경우 ValueError <기본 입출력> >자동차 이름과 시도 횟수 입력받기 ->실행 결과 (전진 횟수 * '-' 사용) ->최종 우승자 + (단, 이름은 5자 이하, 쉼표로 구분) +>실행 결과 (전진 횟수 * '-' 사용) <전진 여부 함수> >0~9 사이 무작위 값 >그 값이 4 이상이면 전진 +<최종 우승자 출력> +>전진 최대값 구하기 +>자동차 전진값 == 최대값인 경우 winner 리스트에 추가 +>형식에 맞추어 출력 \ No newline at end of file From 71dedecc4cbca3da93c83aa9a9be75c84b44b478 Mon Sep 17 00:00:00 2001 From: SW Baek Date: Sat, 8 Feb 2025 12:29:51 +0900 Subject: [PATCH 07/18] [#0] fix `capsys.readouterr()` to have `.out` --- tests/racingcar/test_main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/racingcar/test_main.py b/tests/racingcar/test_main.py index 2bb91be..4c4efcc 100644 --- a/tests/racingcar/test_main.py +++ b/tests/racingcar/test_main.py @@ -19,7 +19,7 @@ def test_전진_및_정지(capsys): main() # 프로그램 실행 # 출력값을 캡처한 후 검증 - 캡처된_출력 = capsys.readouterr() + 캡처된_출력 = capsys.readouterr().out assert all(예상_출력 in 캡처된_출력 for 예상_출력 in ["pobi : -", "woni : ", "최종 우승자 : pobi"]) From 627777a3217f1d6fd5363fc31fc2e2c75d448b74 Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sat, 8 Feb 2025 22:15:07 +0900 Subject: [PATCH 08/18] =?UTF-8?q?fix:=20=EB=B6=88=ED=95=84=EC=9A=94=20?= =?UTF-8?q?=EA=B3=B5=EB=B0=B1=20=EB=B0=8F=20=EB=B3=80=EC=88=98=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/racingcar/main.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index d85b3b7..9e7c295 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -5,39 +5,39 @@ def main(): car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") cars = car_names.split(",") for car in cars : - if (len(car) > 5) : + if len(car) > 5 : raise ValueError("이름이 5자를 초과합니다.") car_positions = {car : 0 for car in cars} attempts = input("시도할 횟수는 몇 회인가요?\n") - if (not attempts.isdigit()) : + if not attempts.isdigit() : raise ValueError("시도 횟수는 숫자로 입력하세요.") attempts = int(attempts) print("\n실행 결과") - for i in range(attempts) : + for _ in range(attempts) : movement(cars, car_positions) for car in cars : print(car + " : " + '-' * car_positions[car]) print() - printWinner(cars, car_positions) - + print_winner(cars, car_positions) + def movement(cars, car_positions) : for car in cars : - if (random.randint(0,9) >= 4) : + if random.randint(0,9) >= 4 : car_positions[car] += 1 -def printWinner(cars, car_positions) : +def print_winner(cars, car_positions) : winners = [] max_value = max(car_positions.values()) - for car in cars : - if (car_positions[car] == max_value) : + for car in cars : + if car_positions[car] == max_value : winners.append(car) - + print(f"최종 우승자 : {', '.join(winners)}") if __name__ == "__main__": - main() + main() \ No newline at end of file From b2cab80c7d4353041512d7913aee585c062b9e7f Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sat, 8 Feb 2025 22:25:21 +0900 Subject: [PATCH 09/18] =?UTF-8?q?fix:=20PEP8=20=EC=8A=A4=ED=83=80=EC=9D=BC?= =?UTF-8?q?=EB=A1=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 | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 9e7c295..e88d92f 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,22 +1,24 @@ import random + + def main(): print("프로그램이 시작되었습니다.") car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") cars = car_names.split(",") - for car in cars : + for car in cars: if len(car) > 5 : raise ValueError("이름이 5자를 초과합니다.") car_positions = {car : 0 for car in cars} attempts = input("시도할 횟수는 몇 회인가요?\n") - if not attempts.isdigit() : + if not attempts.isdigit(): raise ValueError("시도 횟수는 숫자로 입력하세요.") attempts = int(attempts) print("\n실행 결과") - for _ in range(attempts) : + for _ in range(attempts): movement(cars, car_positions) for car in cars : print(car + " : " + '-' * car_positions[car]) @@ -24,20 +26,18 @@ def main(): print_winner(cars, car_positions) -def movement(cars, car_positions) : +def movement(cars, car_positions): for car in cars : if random.randint(0,9) >= 4 : car_positions[car] += 1 -def print_winner(cars, car_positions) : - winners = [] +def get_winner(cars, car_positions): max_value = max(car_positions.values()) + return [car for car in cars if car_positions[car] == max_value] - for car in cars : - if car_positions[car] == max_value : - winners.append(car) - - print(f"최종 우승자 : {', '.join(winners)}") +def print_winner(cars, car_positions): + winner = get_winner(cars, car_positions) + print(f"최종 우승자 : {', '.join(winner)}") if __name__ == "__main__": main() \ No newline at end of file From 844cbd080b3c5fa755b531489b8eda88fdab1df8 Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sat, 8 Feb 2025 23:50:54 +0900 Subject: [PATCH 10/18] =?UTF-8?q?style:=20=EB=8F=85=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=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 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index e88d92f..c25780b 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -2,6 +2,11 @@ def main(): + """ + 프로그램을 실행하고, 자동차 경주의 시뮬레이션을 수행하는 함수 + 사용자로부터 자동차 이름과 시도 횟수를 입력받고, 각 시도마다 + 경주를 진행하여 최종 우승자를 출력. + """ print("프로그램이 시작되었습니다.") car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") @@ -27,11 +32,18 @@ def main(): print_winner(cars, car_positions) def movement(cars, car_positions): + """ + 각 자동차가 0에서 9 사이의 랜덤 숫자를 생성하여, + 4 이상의 숫자가 나오면 해당 자동차의 위치를 1 증가시키는 함수 + """ for car in cars : if random.randint(0,9) >= 4 : car_positions[car] += 1 def get_winner(cars, car_positions): + """ + 경주에서 우승한 자동차를 구하는 함수입니다. + """ max_value = max(car_positions.values()) return [car for car in cars if car_positions[car] == max_value] From 4f16b81cf5eb98f1a3c30d41f9bfdd7b9b320034 Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sat, 8 Feb 2025 23:57:36 +0900 Subject: [PATCH 11/18] =?UTF-8?q?style:=20=EB=8F=85=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=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 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index c25780b..b3f2268 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -42,12 +42,15 @@ def movement(cars, car_positions): def get_winner(cars, car_positions): """ - 경주에서 우승한 자동차를 구하는 함수입니다. + 경주에서 우승한 자동차를 구하는 함수 """ max_value = max(car_positions.values()) return [car for car in cars if car_positions[car] == max_value] def print_winner(cars, car_positions): + """ + 최종 우승자를 출력하는 함수 + """ winner = get_winner(cars, car_positions) print(f"최종 우승자 : {', '.join(winner)}") From eb061ee47fed923b22f3d2147f749d43d77695a7 Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sun, 9 Feb 2025 17:49:57 +0900 Subject: [PATCH 12/18] =?UTF-8?q?style:=20PEP8=EB=A1=9C=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 | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index b3f2268..8e0b7a4 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,3 +1,5 @@ +""" 자동차 경주 게임 """ + import random @@ -11,35 +13,44 @@ def main(): car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") cars = car_names.split(",") + if not all(cars): + raise ValueError("자동차 이름은 빈 문자열이 될 수 없습니다.") + if len(cars) != len(set(cars)): + raise ValueError("중복된 이름은 허용되지 않습니다.") for car in cars: - if len(car) > 5 : + if len(car) > 5: raise ValueError("이름이 5자를 초과합니다.") - car_positions = {car : 0 for car in cars} + car_positions = {car: 0 for car in cars} attempts = input("시도할 횟수는 몇 회인가요?\n") if not attempts.isdigit(): raise ValueError("시도 횟수는 숫자로 입력하세요.") attempts = int(attempts) + if attempts <= 0: + raise ValueError("시도 횟수는 1 이상이어야 합니다.") + print("\n실행 결과") for _ in range(attempts): movement(cars, car_positions) - for car in cars : + for car in cars: print(car + " : " + '-' * car_positions[car]) print() print_winner(cars, car_positions) +FORWARD_THRESHOLD = 4 def movement(cars, car_positions): """ 각 자동차가 0에서 9 사이의 랜덤 숫자를 생성하여, 4 이상의 숫자가 나오면 해당 자동차의 위치를 1 증가시키는 함수 """ - for car in cars : - if random.randint(0,9) >= 4 : + for car in cars: + if random.randint(0, 9) >= FORWARD_THRESHOLD: car_positions[car] += 1 + def get_winner(cars, car_positions): """ 경주에서 우승한 자동차를 구하는 함수 @@ -47,6 +58,8 @@ def get_winner(cars, car_positions): max_value = max(car_positions.values()) return [car for car in cars if car_positions[car] == max_value] + + def print_winner(cars, car_positions): """ 최종 우승자를 출력하는 함수 @@ -55,4 +68,5 @@ def print_winner(cars, car_positions): print(f"최종 우승자 : {', '.join(winner)}") if __name__ == "__main__": - main() \ No newline at end of file + # 프로그램이 직접 실행될 때만 main() 함수를 호출 + main() From 9df4855de1fff7012a527ba042c1d72c7bffa56e Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sun, 9 Feb 2025 18:01:34 +0900 Subject: [PATCH 13/18] =?UTF-8?q?feat:=20=EC=B6=94=EA=B0=80=20=EC=8B=9C?= =?UTF-8?q?=EB=82=98=EB=A6=AC=EC=98=A4=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/racingcar/test_main.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/racingcar/test_main.py b/tests/racingcar/test_main.py index 4c4efcc..7738ca5 100644 --- a/tests/racingcar/test_main.py +++ b/tests/racingcar/test_main.py @@ -34,5 +34,26 @@ def test_이름에_대한_예외(): with patch('builtins.input', side_effect=["pobi,javaji", "1"]): main() # 프로그램 실행 - # 추가적인 시나리오에 대한 테스트를 필요에 따라 작성할 수 있습니다. + +# 이름에 대한 예외 처리 테스트 +def test_시도에_대한_예외(): + """ + 시도 횟수에 대한 예외 처리를 테스트합니다. + 'pytest.raises'를 사용해 IllegalArgumentException과 유사한 ValueError를 발생시키는지 검증합니다. + """ + with pytest.raises(ValueError): + # 자동차 이름이 잘못된 경우에 대한 입력을 모의 처리 + with patch('builtins.input', side_effect=["pobi,javaji", "0"]): + main() # 프로그램 실행 + +# 이름의 중복에 대한 예외 처리 테스트 +def test_중복에_대한_예외(): + """ + 이름 중복에 대한 예외 처리를 테스트합니다. + 'pytest.raises'를 사용해 IllegalArgumentException과 유사한 ValueError를 발생시키는지 검증합니다. + """ + with pytest.raises(ValueError): + # 자동차 이름이 잘못된 경우에 대한 입력을 모의 처리 + with patch('builtins.input', side_effect=["pobi,javaji,pobi", "1"]): + main() # 프로그램 실행 From c52505616b64857f75aaf7bb0f20faae291691be Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sun, 9 Feb 2025 18:01:51 +0900 Subject: [PATCH 14/18] =?UTF-8?q?style:=20PEP8=EB=A1=9C=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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 8e0b7a4..8ef84bb 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -1,13 +1,14 @@ """ 자동차 경주 게임 """ import random +FORWARD_THRESHOLD = 4 def main(): """ 프로그램을 실행하고, 자동차 경주의 시뮬레이션을 수행하는 함수 - 사용자로부터 자동차 이름과 시도 횟수를 입력받고, 각 시도마다 - 경주를 진행하여 최종 우승자를 출력. + 사용자로부터 자동차 이름과 시도 횟수를 입력받고, + 각 시도마다 경주를 진행하여 최종 우승자를 출력. """ print("프로그램이 시작되었습니다.") @@ -29,7 +30,6 @@ def main(): attempts = int(attempts) if attempts <= 0: raise ValueError("시도 횟수는 1 이상이어야 합니다.") - print("\n실행 결과") for _ in range(attempts): @@ -37,10 +37,9 @@ def main(): for car in cars: print(car + " : " + '-' * car_positions[car]) print() - print_winner(cars, car_positions) -FORWARD_THRESHOLD = 4 + def movement(cars, car_positions): """ 각 자동차가 0에서 9 사이의 랜덤 숫자를 생성하여, @@ -67,6 +66,7 @@ def print_winner(cars, car_positions): winner = get_winner(cars, car_positions) print(f"최종 우승자 : {', '.join(winner)}") + if __name__ == "__main__": # 프로그램이 직접 실행될 때만 main() 함수를 호출 main() From ea836b141334cfa9e00f70ec9faa1d3a17796ffd Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sun, 9 Feb 2025 18:20:49 +0900 Subject: [PATCH 15/18] =?UTF-8?q?feat:=20=EC=B6=94=EA=B0=80=20=EC=8B=9C?= =?UTF-8?q?=EB=82=98=EB=A6=AC=EC=98=A4=EC=97=90=20=EB=8C=80=ED=95=9C=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/racingcar/test_main.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/racingcar/test_main.py b/tests/racingcar/test_main.py index 7738ca5..d0b4181 100644 --- a/tests/racingcar/test_main.py +++ b/tests/racingcar/test_main.py @@ -47,6 +47,7 @@ def test_시도에_대한_예외(): with patch('builtins.input', side_effect=["pobi,javaji", "0"]): main() # 프로그램 실행 + # 이름의 중복에 대한 예외 처리 테스트 def test_중복에_대한_예외(): """ @@ -55,5 +56,29 @@ def test_중복에_대한_예외(): """ with pytest.raises(ValueError): # 자동차 이름이 잘못된 경우에 대한 입력을 모의 처리 - with patch('builtins.input', side_effect=["pobi,javaji,pobi", "1"]): + with patch('builtins.input', side_effect=["pobi,won,pobi", "1"]): main() # 프로그램 실행 + + +# 빈 이름에 대한 예외 처리 테스트 +def test_빈_문자열에_대한_예외(): + """ + 빈 문자열에 대한 예외 처리를 테스트합니다. + 'pytest.raises'를 사용해 IllegalArgumentException과 유사한 ValueError를 발생시키는지 검증합니다. + """ + with pytest.raises(ValueError): + # 자동차 이름이 잘못된 경우에 대한 입력을 모의 처리 + with patch('builtins.input', side_effect=["pobi,won, ", "1"]): + main() # 프로그램 실행 + + +# 시도 횟수에 문자열 입력에 대한 예외 처리 테스트 +def test_시도_횟수에_문자열_입력에_대한_예외(): + """ + 시도 횟수에 문자열 입력에 대한 예외 처리를 테스트합니다. + 'pytest.raises'를 사용해 IllegalArgumentException과 유사한 ValueError를 발생시키는지 검증합니다. + """ + with pytest.raises(ValueError): + # 시도 횟수에 문자열이 입력된 경우에 대한 입력을 모의 처리 + with patch('builtins.input', side_effect=["pobi, ,won", "abc"]): + main() # 프로그램 실행 \ No newline at end of file From 94ab73afd41f91ae1ce21f8df75171f99f753a17 Mon Sep 17 00:00:00 2001 From: chaerishme Date: Sun, 9 Feb 2025 18:21:33 +0900 Subject: [PATCH 16/18] =?UTF-8?q?style:=20PEP8=EB=A1=9C=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 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/racingcar/main.py b/src/racingcar/main.py index 8ef84bb..19c558f 100644 --- a/src/racingcar/main.py +++ b/src/racingcar/main.py @@ -14,7 +14,7 @@ def main(): car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n") cars = car_names.split(",") - if not all(cars): + if not all(car.strip() for car in cars): raise ValueError("자동차 이름은 빈 문자열이 될 수 없습니다.") if len(cars) != len(set(cars)): raise ValueError("중복된 이름은 허용되지 않습니다.") @@ -42,7 +42,7 @@ def main(): def movement(cars, car_positions): """ - 각 자동차가 0에서 9 사이의 랜덤 숫자를 생성하여, + 각 자동차가 0에서 9 사이의 랜덤 숫자를 생성하여, 4 이상의 숫자가 나오면 해당 자동차의 위치를 1 증가시키는 함수 """ for car in cars: @@ -58,7 +58,6 @@ def get_winner(cars, car_positions): return [car for car in cars if car_positions[car] == max_value] - def print_winner(cars, car_positions): """ 최종 우승자를 출력하는 함수 From ea06b100b10d5eb3585860df85aee4639c01e626 Mon Sep 17 00:00:00 2001 From: chaerishme Date: Tue, 11 Feb 2025 22:10:06 +0900 Subject: [PATCH 17/18] =?UTF-8?q?refactor:=20=ED=95=A8=EC=88=98=EB=AA=85?= =?UTF-8?q?=20=EB=B0=8F=20=EC=A3=BC=EC=84=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 명확한 설명을 위한 함수명 및 주석 변경 test 타겟만 검증하도록 수정 --- tests/racingcar/test_main.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/racingcar/test_main.py b/tests/racingcar/test_main.py index d0b4181..71e3e22 100644 --- a/tests/racingcar/test_main.py +++ b/tests/racingcar/test_main.py @@ -36,38 +36,38 @@ def test_이름에_대한_예외(): # 추가적인 시나리오에 대한 테스트를 필요에 따라 작성할 수 있습니다. -# 이름에 대한 예외 처리 테스트 -def test_시도에_대한_예외(): +# 잘못된 시도 횟수에 대한 예외 처리 테스트 +def test_시도_횟수에_대한_예외(): """ 시도 횟수에 대한 예외 처리를 테스트합니다. 'pytest.raises'를 사용해 IllegalArgumentException과 유사한 ValueError를 발생시키는지 검증합니다. """ with pytest.raises(ValueError): - # 자동차 이름이 잘못된 경우에 대한 입력을 모의 처리 - with patch('builtins.input', side_effect=["pobi,javaji", "0"]): + # 시도 횟수가 0인 경우에 대한 입력을 모의 처리 + with patch('builtins.input', side_effect=["pobi,won", "0"]): main() # 프로그램 실행 # 이름의 중복에 대한 예외 처리 테스트 -def test_중복에_대한_예외(): +def test_이름_중복에_대한_예외(): """ 이름 중복에 대한 예외 처리를 테스트합니다. 'pytest.raises'를 사용해 IllegalArgumentException과 유사한 ValueError를 발생시키는지 검증합니다. """ with pytest.raises(ValueError): - # 자동차 이름이 잘못된 경우에 대한 입력을 모의 처리 + # 자동차 이름이 중복된 경우에 대한 입력을 모의 처리 with patch('builtins.input', side_effect=["pobi,won,pobi", "1"]): main() # 프로그램 실행 # 빈 이름에 대한 예외 처리 테스트 -def test_빈_문자열에_대한_예외(): +def test_자동차_이름이_빈_경우에_대한_예외(): """ - 빈 문자열에 대한 예외 처리를 테스트합니다. + 자동차 이름이 빈 경우에 대한 예외 처리를 테스트합니다. 'pytest.raises'를 사용해 IllegalArgumentException과 유사한 ValueError를 발생시키는지 검증합니다. """ with pytest.raises(ValueError): - # 자동차 이름이 잘못된 경우에 대한 입력을 모의 처리 + # 자동차 이름이 빈 경우에 대한 입력을 모의 처리 with patch('builtins.input', side_effect=["pobi,won, ", "1"]): main() # 프로그램 실행 @@ -80,5 +80,5 @@ def test_시도_횟수에_문자열_입력에_대한_예외(): """ with pytest.raises(ValueError): # 시도 횟수에 문자열이 입력된 경우에 대한 입력을 모의 처리 - with patch('builtins.input', side_effect=["pobi, ,won", "abc"]): + with patch('builtins.input', side_effect=["pobi,won", "abc"]): main() # 프로그램 실행 \ No newline at end of file From bbe7a9bb7ecbbdf17f9f1eb59075382f5dbacd83 Mon Sep 17 00:00:00 2001 From: chaerishme Date: Tue, 11 Feb 2025 22:17:48 +0900 Subject: [PATCH 18/18] =?UTF-8?q?docs:=20=EB=A7=88=ED=81=AC=EB=8B=A4?= =?UTF-8?q?=EC=9A=B4=20=EB=AC=B8=EB=B2=95=20=EC=A0=81=EA=B7=B9=EC=A0=81=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [], # 등 사용되도록 수정 --- docs/README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/README.md b/docs/README.md index 9ff93b7..2c1fc52 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,18 +1,18 @@ -<조건> +#### 조건 >들여쓰기 깊이 2까지만 허용 >함수나 메서드가 한 가지 일만 하도록 최대한 작게 만들기 >입력이 올바르지 않은 경우 ValueError -<기본 입출력> ->자동차 이름과 시도 횟수 입력받기 +#### 기본 입출력 +* [x] 자동차 이름과 시도 횟수 입력받기 (단, 이름은 5자 이하, 쉼표로 구분) ->실행 결과 (전진 횟수 * '-' 사용) +* [x] 실행 결과 (전진 횟수 * '-' 사용) -<전진 여부 함수> ->0~9 사이 무작위 값 ->그 값이 4 이상이면 전진 +#### 전진 여부 함수 +* [x] 0~9 사이 무작위 값 +* [x] 그 값이 4 이상이면 전진 -<최종 우승자 출력> ->전진 최대값 구하기 ->자동차 전진값 == 최대값인 경우 winner 리스트에 추가 ->형식에 맞추어 출력 \ No newline at end of file +#### 최종 우승자 출력 +* [x] 전진 최대값 구하기 +* [x] 자동차 전진값 == 최대값인 경우 winner 리스트에 추가 +* [x] 형식에 맞추어 출력 \ No newline at end of file