鱼C论坛

 找回密码
 立即注册
查看: 1249|回复: 4

[已解决]【求助】关于MatchAnalysis的一个题目

[复制链接]
发表于 2022-2-6 22:18:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 神愿 于 2022-2-6 22:22 编辑
  1. from random import random
  2. def printIntro():
  3.     print('这个程序模拟两个选手A和B的某种竞技比赛')
  4.     print('程序运行需要A和B的能力值(以0到1之间的小数表示)')
  5. def getInputs():
  6.     a = eval(input('请输入选手A的能力值(0-1):'))
  7.     b = eval(input('请输入选手B的能力值(0-1):'))
  8.     n = eval(input('模拟比赛场次:'))
  9.     return a, b, n
  10. def simNGames(n, probA, probB):
  11.     winsA, winsB = 0, 0
  12.     for i in range(n):
  13.         scoreA, scoreB = simOneGame(probA, probB)
  14.         if scoreA > scoreB:
  15.             winsA += 1
  16.         else:
  17.             winsB += 1
  18.         return winsA, winsB
  19. def gameOver(a,b):
  20.     return a==15 or b==15
  21. def simOneGame(probA, probB):
  22.     scoreA, scoreB = 0, 0
  23.     serving = "A"
  24.     while not gameOver(scoreA, scoreB):
  25.         if serving == "A":
  26.             if random() < probA:
  27.                 scoreA += 1
  28.             else:
  29.                 serving = 'B'
  30.         else:
  31.             if random() < probB:
  32.                 scoreB += 1
  33.             else:
  34.                 serving = "A"
  35.     return scoreA, scoreB
  36. def printSummary(winsA, winsB):
  37.     n = winsA + winsB
  38.     print("竞技分析开始,共模拟{}场比赛".format(n))
  39.     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
  40.     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
  41. def main():
  42.     printIntro()
  43.     probA, probB, n = getInputs()
  44.     winsA, winsB = simNGames(n, probA, probB)
  45.     printSummary(winsA, winsB)
  46. main()
  47.    
复制代码



无论我输入比赛多少场,它都只比赛一场就结束了
我查看了很久,真的不知道是哪里出了问题
如果您不介意我占用您五分钟时间,可以帮我看看,我哪里错了吗?

谢谢谢谢谢谢谢谢谢!!
最佳答案
2022-2-6 22:25:03

因为你将 simNGames  函数的 return 放入了 for 循环中,导致执行一次循环就遇到 return 返回了

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

参考代码:
  1. from random import random


  2. def printIntro():
  3.     print('这个程序模拟两个选手A和B的某种竞技比赛')
  4.     print('程序运行需要A和B的能力值(以0到1之间的小数表示)')


  5. def getInputs():
  6.     a = eval(input('请输入选手A的能力值(0-1):'))
  7.     b = eval(input('请输入选手B的能力值(0-1):'))
  8.     n = eval(input('模拟比赛场次:'))
  9.     return a, b, n


  10. def simNGames(n, probA, probB):
  11.     winsA, winsB = 0, 0
  12.     for i in range(n):
  13.         scoreA, scoreB = simOneGame(probA, probB)
  14.         if scoreA > scoreB:
  15.             winsA += 1
  16.         else:
  17.             winsB += 1
  18.     return winsA, winsB


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


  21. def simOneGame(probA, probB):
  22.     scoreA, scoreB = 0, 0
  23.     serving = "A"
  24.     while not gameOver(scoreA, scoreB):
  25.         if serving == "A":
  26.             if random() < probA:
  27.                 scoreA += 1
  28.             else:
  29.                 serving = 'B'
  30.         else:
  31.             if random() < probB:
  32.                 scoreB += 1
  33.             else:
  34.                 serving = "A"
  35.     return scoreA, scoreB


  36. def printSummary(winsA, winsB):
  37.     n = winsA + winsB
  38.     print("竞技分析开始,共模拟{}场比赛".format(n))
  39.     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA / n))
  40.     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB / n))


  41. def main():
  42.     printIntro()
  43.     probA, probB, n = getInputs()
  44.     winsA, winsB = simNGames(n, probA, probB)
  45.     printSummary(winsA, winsB)


  46. main()

复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-6 22:25:03 | 显示全部楼层    本楼为最佳答案   

因为你将 simNGames  函数的 return 放入了 for 循环中,导致执行一次循环就遇到 return 返回了

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

参考代码:
  1. from random import random


  2. def printIntro():
  3.     print('这个程序模拟两个选手A和B的某种竞技比赛')
  4.     print('程序运行需要A和B的能力值(以0到1之间的小数表示)')


  5. def getInputs():
  6.     a = eval(input('请输入选手A的能力值(0-1):'))
  7.     b = eval(input('请输入选手B的能力值(0-1):'))
  8.     n = eval(input('模拟比赛场次:'))
  9.     return a, b, n


  10. def simNGames(n, probA, probB):
  11.     winsA, winsB = 0, 0
  12.     for i in range(n):
  13.         scoreA, scoreB = simOneGame(probA, probB)
  14.         if scoreA > scoreB:
  15.             winsA += 1
  16.         else:
  17.             winsB += 1
  18.     return winsA, winsB


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


  21. def simOneGame(probA, probB):
  22.     scoreA, scoreB = 0, 0
  23.     serving = "A"
  24.     while not gameOver(scoreA, scoreB):
  25.         if serving == "A":
  26.             if random() < probA:
  27.                 scoreA += 1
  28.             else:
  29.                 serving = 'B'
  30.         else:
  31.             if random() < probB:
  32.                 scoreB += 1
  33.             else:
  34.                 serving = "A"
  35.     return scoreA, scoreB


  36. def printSummary(winsA, winsB):
  37.     n = winsA + winsB
  38.     print("竞技分析开始,共模拟{}场比赛".format(n))
  39.     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA / n))
  40.     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB / n))


  41. def main():
  42.     printIntro()
  43.     probA, probB, n = getInputs()
  44.     winsA, winsB = simNGames(n, probA, probB)
  45.     printSummary(winsA, winsB)


  46. main()

复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-6 22:26:15 | 显示全部楼层
  1. from random import random


  2. def printIntro():
  3.     print("这个程序模拟两个选手A和B的某种竞技比赛")
  4.     print("程序运行需要A和B的能力值(以0到1之间的小数表示)")


  5. def getInputs():
  6.     a = eval(input("请输入选手A的能力值(0-1):"))
  7.     b = eval(input("请输入选手B的能力值(0-1):"))
  8.     n = eval(input("模拟比赛场次:"))
  9.     return a, b, n


  10. def simNGames(n, probA, probB):
  11.     winsA, winsB = 0, 0
  12.     for i in range(n):
  13.         scoreA, scoreB = simOneGame(probA, probB)
  14.         if scoreA > scoreB:
  15.             winsA += 1
  16.         else:
  17.             winsB += 1
  18.     return winsA, winsB # 我改了下这里,return 应该不是在 for 循环里


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


  21. def simOneGame(probA, probB):
  22.     scoreA, scoreB = 0, 0
  23.     serving = "A"
  24.     while not gameOver(scoreA, scoreB):
  25.         if serving == "A":
  26.             if random() < probA:
  27.                 scoreA += 1
  28.             else:
  29.                 serving = "B"
  30.         else:
  31.             if random() < probB:
  32.                 scoreB += 1
  33.             else:
  34.                 serving = "A"
  35.     return scoreA, scoreB


  36. def printSummary(winsA, winsB):
  37.     n = winsA + winsB
  38.     print("竞技分析开始,共模拟{}场比赛".format(n))
  39.     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA / n))
  40.     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB / n))


  41. def main():
  42.     printIntro()
  43.     probA, probB, n = getInputs()
  44.     winsA, winsB = simNGames(n, probA, probB)
  45.     printSummary(winsA, winsB)


  46. main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-8 18:32:44 | 显示全部楼层

谢谢帮助!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-8 18:33:51 | 显示全部楼层
Twilight6 发表于 2022-2-6 22:25
因为你将 simNGames  函数的 return 放入了 for 循环中,导致执行一次循环就遇到 return 返回了

def s ...

是缩进的问题,对吗?谢谢您!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 10:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表