2013-05-09 2 views
0

raquetball 시뮬레이션을위한 숙제가 있습니다. 나는 프로그램을 확장하여 셧아웃을 설명하고 각 플레이어의 수를 반환하는 방법을 알아 내려고 노력 중이다. 나는 시뮬레이션을 계산하기 위해 simNGames()에 루프를 추가했다. 나는 그 값들을 반환하고 그것을 요약본에 인쇄하고 싶습니다.파이썬 3의 함수에서이 반환 값을 어떻게 출력합니까?

def simNGames(n, probA, probB): 
    winsA = 0 
    winsB = 0 
    shutoutA = 0 
    shutoutB = 0 
    for i in range(n): 
     scoreA, scoreB = simOneGame(probA, probB) 
     if scoreA > scoreB: 
      winsA = winsA + 1 
     else: 
      winsB = winsB + 1 
    for i in range(n): 
     scoreA, scoreB = simOneGame(probA, probB) 
     if scoreA == 15 and scoreB == 0: 
      shutoutA = shutoutA + 1 
     if scoreA == 0 and scoreB == 15: 
      shutoutB = shutoutB + 1 

     return winsA, winsB, shutoutA, shutoutB ## The program breaks when I add 
               ## shutoutA, and shutoutB as return val            

누구든지 올바른 방향으로 나를 조종 할 수 있다면 크게 감사하겠습니다. ValueError : 반환 값에 셧다운을 추가 할 때 너무 많은 값을 풀어야합니다 (예상 2). 여기에 전체 프로그램은 다음과 같습니다

당신이 함수를 호출
from random import random 

def main(): 
    probA, probB, n = GetInputs() 
    winsA, winsB = simNGames(n, probA, probB) 
    PrintSummary(winsA, winsB) 


def GetInputs(): 
    a = eval(input("What is the probability player A wins the serve? ")) 
    b = eval(input("What is the probablity player B wins the serve? ")) 
    n = eval(input("How many games are they playing? ")) 
    return a, b, n 



def simNGames(n, probA, probB): 
    winsA = 0 
    winsB = 0 
    shutoutA = 0 
    shutoutB = 0 
    for i in range(n): 
     scoreA, scoreB = simOneGame(probA, probB) 
     if scoreA > scoreB: 
      winsA = winsA + 1 
     else: 
      winsB = winsB + 1 
    for i in range(n): 
     scoreA, scoreB = simOneGame(probA, probB) 
     if scoreA == 15 and scoreB == 0: 
      shutoutA = shutoutA + 1 
     if scoreA == 0 and scoreB == 15: 
      shutoutB = shutoutB + 1 

     return winsA, winsB 

def simOneGame(probA, probB): 
    serving = "A" 
    scoreA = 0 
    scoreB = 0 
    while not gameOver(scoreA, scoreB): 
     if serving == "A": 
      if random() < probA: 
       scoreA = scoreA + 1 
      else: 
       serving = "B" 

     else: 
      if random() < probB: 
       scoreB = scoreB + 1 
      else: 
       serving = "A" 
    return scoreA, scoreB 

def gameOver(a, b): 
    return a == 15 or b == 15 

def PrintSummary(winsA, winsB): 
    n = winsA + winsB 
    print("\nGames simulated:", n) 
    print("Wins for A: {0} ({1:0.1%})".format(winsA, winsA/n)) 
    print("Wins for B: {0} ({1:0.1%})".format(winsB, winsB/n)) 


if __name__ == '__main__': main() 

답변

2

:

winsA, winsB = simNGames(n, probA, probB) 

당신은 두 개의 값 (winsA, winsB)를 기대하지만, 네를 반환하고 있습니다.

+0

@ Lee Daniel Crocker. 고맙습니다! 여러 함수를 사용하면 매우 혼란 스러울 수 있습니다. –

관련 문제