Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions src/baseball/main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import random

def player_input() :
while True :
player_number = input("3자리 숫자를 입력하세요: ")
if len(player_number) != 3 or not player_number.isdigit():
print("입력은 3자리 숫자여야 합니다.")
continue
elif len(set(player_number)) != 3:
print("입력 값의 각 자리는 서로 달라야 합니다.")
continue
break

player_number = input("숫자를 입력해주세요: ")
if len(player_number) != 3 or not player_number.isdigit():
raise ValueError("입력은 3자리 숫자여야 합니다.")

elif len(set(player_number)) != 3:
raise ValueError("입력 값의 각 자리는 서로 달라야 합니다.")
return player_number


def game_comp(player_number) :
def game_comp(com_number, player_number) :
strike, ball, index = 0, 0, 0

for i in com_number :
Expand All @@ -25,31 +23,36 @@ def game_comp(player_number) :
return strike, ball


def game_print() :
def game_print(com_number) :
while True:
number = player_input()
strike, ball = game_comp(number)
print(strike, "스트라이크", " / ", ball, "볼")

if strike == 3 :
print("축하합니다! 숫자를 모두 맞추셨습니다.")
strike, ball = game_comp(com_number, number)
if strike==0 and ball==0:
print("낫싱")
continue
elif strike == 3 :
print(strike,"스트라이크")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print문에서 ,를 쓰게 되면 무조건 두 변수 사이에 하나의 공백이 들어가게 됩니다.
공백을 의도해서 사용할 때 아니면 string의 +연산이나 fstring을 사용해주세요.

print("3개의 숫자를 모두 맞히셨습니다! 게임 종료")
break
else:
print(ball,"볼"," ",strike,"스트라이크")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(동일 이슈)
print문에서 ,를 쓰게 되면 무조건 두 변수 사이에 하나의 공백이 들어가게 됩니다.
공백을 의도해서 사용할 때 아니면 string의 +연산이나 fstring을 사용해주세요.

continue

def main():
while True :
com_number = random.sample(range(1,10),3)
print("숫자 야구 게임을 시작합니다.")
game_print(com_number)

while True :
com_number = random.sample(range(1,10),3)
print("숫자 야구 게임을 시작합니다.")
game_print()

while True:
result = input("게임을 다시 시작하려면 1, 완전히 종료하려면 2를 입력하세요: ")

result = input("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요.")
if result == '1':
print("게임을 다시 시작합니다.")
break # 이곳에서 안쪽 while 문을 종료하고 게임을 다시 시작
main()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main()문을 다시 실행해버리면 n번째 main으로 깊게 들어가버릴 수 있어요.
그러면 정상 종료가 힘들어 지겠죠?

여기선 continue를 써주면 매끄럽게 게임을 재진행합니다

elif result == '2':
print("게임을 종료합니다.")
exit() # 프로그램을 종료
exit()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit()보단 return을 활용해서 해당 함수를 탈출해 상위 함수로 가는 것으로 생각해봅시다
(early return)

else:
print("잘못된 입력입니다. 1 또는 2를 입력하세요.")
raise ValueError("잘못된 입력입니다. 1 또는 2를 입력하세요.")

if __name__ == "__main__":
main()