파이썬 가위바위보 만들기 질문
조회수 1839회
import random win=0 draw=0 lose=0
while 1: com=random.randint(0,2) print('컴퓨터:',com) a=int(input('0:가위 1:바위 2:보자기: ')) res=com-a
if res==0:
print('draw \n')
if res==-1 or res==2:
print('win \n')
win=win+1
if res==1 or res==-2:
print('lose \n')
lose=lose+1
여기까지 동작은잘됩니다 근데 여기서 9를 입력하면 몇전 몇승 몇패 승률 몇%인지 출력하고 끝내야하는데 잘안되네요 어떻게해야하나요?
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
마침 예전 질문글 때문에 JS로 가위바위보 데모를 짰었는데 그때 승률 구현을 해놓길 잘했네요. 간단히 얼개만 보여드리자면:
var totalGames = 0 var wonGames = 0 // 중략... .on(뭔가 입력을 했을 때, function () { if (입력이 9가 아니라면) { totalGames++ // 중략... if (이겼을때) { wonGames++ } // 중략... } else { // 시작하자마자 9를 입력해 종료가 될 경우 0으로 나누기 에러가 일어날 수 있다. if (totalGames == 0) { totalGames = 1 wonGames = 0 } alert('내 승률: ' + (wonGames * 100 / totalGames) + '%') // 이하 생략... } })
댓글 입력