Skip to content

Commit

Permalink
style: PEP8로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
chaerishme committed Feb 9, 2025
1 parent 2aec5d9 commit f1f3351
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/racingcar/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
""" 자동차 경주 게임 """

import random


Expand All @@ -11,42 +13,53 @@ def main():

car_names = input("경주할 자동차 이름을 입력하세요.(이름은 쉼표로 구분)\n")
cars = car_names.split(",")
if not all(cars):
raise ValueError("자동차 이름은 빈 문자열이 될 수 없습니다.")

Check warning on line 17 in src/racingcar/main.py

View check run for this annotation

Codecov / codecov/patch

src/racingcar/main.py#L17

Added line #L17 was not covered by tests
if len(cars) != len(set(cars)):
raise ValueError("중복된 이름은 허용되지 않습니다.")

Check warning on line 19 in src/racingcar/main.py

View check run for this annotation

Codecov / codecov/patch

src/racingcar/main.py#L19

Added line #L19 was not covered by tests
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("시도 횟수는 숫자로 입력하세요.")

Check warning on line 28 in src/racingcar/main.py

View check run for this annotation

Codecov / codecov/patch

src/racingcar/main.py#L28

Added line #L28 was not covered by tests
attempts = int(attempts)
if attempts <= 0:
raise ValueError("시도 횟수는 1 이상이어야 합니다.")

Check warning on line 31 in src/racingcar/main.py

View check run for this annotation

Codecov / codecov/patch

src/racingcar/main.py#L31

Added line #L31 was not covered by tests


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):
"""
경주에서 우승한 자동차를 구하는 함수
"""
max_value = max(car_positions.values())
return [car for car in cars if car_positions[car] == max_value]



def print_winner(cars, car_positions):
"""
최종 우승자를 출력하는 함수
Expand All @@ -55,4 +68,5 @@ def print_winner(cars, car_positions):
print(f"최종 우승자 : {', '.join(winner)}")

if __name__ == "__main__":
main()
# 프로그램이 직접 실행될 때만 main() 함수를 호출
main()

0 comments on commit f1f3351

Please sign in to comment.