【问题标题】:How do I print these return values from function in Python 3?如何从 Python 3 中的函数打印这些返回值?
【发布时间】:2013-05-09 19:16:13
【问题描述】:

我有一项关于壁球模拟的家庭作业。我试图弄清楚如何扩展程序以解决停工问题并返回每个玩家的号码。我在 simNGames() 中添加了一个循环来计算关闭次数。我想返回这些值并将它们打印在摘要中。

    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: too many values to unpack (expected 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()   

【问题讨论】:

    标签: python function python-3.x return


    【解决方案1】:

    调用函数时:

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

    您只期望两个值(winsA、winsB),但返回四个。

    【讨论】:

    • @李丹尼尔克罗克。谢谢!使用多个函数可能会非常混乱。
    猜你喜欢
    • 2020-01-29
    • 2015-12-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多