Skip to content
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

[develop] numberbaseball #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
56 changes: 56 additions & 0 deletions numberbaseball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import random

def generate_num(): #랜덤한 숫자만드는 함수
num=['0','0','0',] #숫자 담을 리스트
newnum=0
num[0]=str(random.randrange(1,10,1))
num[1]=str(random.randrange(1,10,1))
num[2]=str(random.randrange(1,10,1))
print("랜덤한 숫자 선택")

while(num[0] == num[1]):
num[1] = str(random.randrange(1, 10, 1))
while(num[0] == num[2] or num[1] == num[2]):
num[2] = str(random.randrange(1, 10, 1))
print (num)
return num

rightanswer=generate_num()
tries=0
strike_number = 0
ball_number = 0

print("숫자 야구 게임을 시작합니다.")
while (strike_number < 3):


number = str(input("숫자를 입력해주세요 :"))

strike_number = 0
ball_number = 0


for i in range(0, 3):
for j in range(0, 3):
if(number[i] == str(rightanswer[j]) and i == j):
strike_number += 1
elif(number[i] == str(rightanswer[j]) and i != j):
ball_number += 1

if(strike_number==3):
print("3스트라이크")
print("3개의 숫자를 모두 맞히셨습니다! 게임 종료")

elif(strike_number==0 and ball_number==0):
print("낫싱")
else:
print("결과: ",strike_number,"스트라이크 ",ball_number,"볼")
tries += 1

print("정답 {}번 만에 맞췄습니다.".format(tries))