吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1719|回复: 7
收起左侧

[求助] 小白初学python,刚学着写程序,卡在书中体育竞技程序设计实例了。

[复制链接]
wuxi 发表于 2021-8-6 23:44
我的程序如下:
from random import random
def intro():
    print('''此程序模拟两个选手A和B的乒乓球比赛
            程序的运行需要A和B的能力值(以0-1之间的小数表示)''')
def getinputs():
    a=eval(input("请输入A的能力值:"))
    b=eval(input("请输入B的能力值:"))
    c=eval(input("请输入比赛的场数:  次"))
    return a, b, c
def Pgames(probA,probB,n):
    winsA,winsB = 0, 0
    for i in range(n):
        scoreA,scoreB = Pogames(probA,probB)
        if scoreA>scoreB:
            winsA += 1
        #elif scoreA<scoreB:
        #    winsB += 1
        else:
            #continue
            winsB += 1
    return winsA,winsB
def gameover(scoreA,scoreB):
    return scoreA ==15 or scoreB == 15
def Pogames(probA,probB):
    scoreA,scoreB = 0, 0
    serving = "A"
    while not gameover(scoreA,scoreB):
        if serving == "A":
            if random() < probA:
                scoreA += 1
            else:
                serving == "B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving =="A"
    return scoreA, scoreB

def summary(a,b):
    n = a + b
    print("共举行{}场比赛".format(n))
    print("选手A获胜{}场,占比{:0.1%}".format(a,a//n))
    print("选手B获胜{}场,占比{:0.1%}".format(b,b//n))
def main():
    intro()
    probA,probB,n = getinputs()
    winsA,winsB = Pgames(probA,probB,n)
    summary(winsA,winsB)
main()
————————分割线————————
但是我得到的结果确是这样的
此程序模拟两个选手A和B的乒乓球比赛
            程序的运行需要A和B的能力值(以0-1之间的小数表示)
请输入A的能力值:0.45
请输入B的能力值:0.5
请输入比赛的场数:  次1000
共举行1000场比赛
选手A获胜1000场,占比100.0%
选手B获胜0场,占比0.0%
>>>
哪出错了 啊呜呜呜呜呜呜呜呜

3

3

1

1

2

2

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
为之奈何? + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ShiratoriAira 发表于 2021-8-7 01:22
本帖最后由 ShiratoriAira 于 2021-8-7 01:30 编辑

测试环境:Python 3.8

如果是Python 2.x,第一行加上
[Python] 纯文本查看 复制代码
from __future__ import division


第42行
[Python] 纯文本查看 复制代码
serving == "B"

改为
[Python] 纯文本查看 复制代码
serving = "B"


第47行
[Python] 纯文本查看 复制代码
serving == "A"

改为
[Python] 纯文本查看 复制代码
serving = "A"

第54-55行
[Python] 纯文本查看 复制代码
print("选手A获胜{}场,占比{:0.1%}".format(a, a // n))
print("选手B获胜{}场,占比{:0.1%}".format(b, b // n))

改为
[Python] 纯文本查看 复制代码
print("选手A获胜{}场,占比{:0.1%}".format(a, a / n))
print("选手B获胜{}场,占比{:0.1%}".format(b, b / n))

[Python] 纯文本查看 复制代码
from random import random


def intro():
    print('''此程序模拟两个选手A和B的乒乓球比赛
            程序的运行需要A和B的能力值(以0-1之间的小数表示)''')


def getinputs():
    a = eval(input("请输入A的能力值:"))
    b = eval(input("请输入B的能力值:"))
    c = eval(input("请输入比赛的场数:  次"))
    return a, b, c


def Pgames(probA, probB, n):
    winsA, winsB = 0, 0
    for i in range(n):
        scoreA, scoreB = Pogames(probA, probB)
        if scoreA > scoreB:
            winsA += 1
        # elif scoreA<scoreB:
        #    winsB += 1
        else:
            # continue
            winsB += 1
    return winsA, winsB


def gameover(scoreA, scoreB):
    return scoreA == 15 or scoreB == 15


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


def summary(a, b):
    n = a + b
    print("共举行{}场比赛".format(n))
    print("选手A获胜{}场,占比{:0.1%}".format(a, a / n))
    print("选手B获胜{}场,占比{:0.1%}".format(b, b / n))


def main():
    intro()
    probA, probB, n = getinputs()
    winsA, winsB = Pgames(probA, probB, n)
    summary(winsA, winsB)


main()



[Bash shell] 纯文本查看 复制代码
此程序模拟两个选手A和B的乒乓球比赛
            程序的运行需要A和B的能力值(以0-1之间的小数表示)
请输入A的能力值:0.45
请输入B的能力值:0.5
请输入比赛的场数:  次1000
共举行1000场比赛
选手A获胜336场,占比33.6%
选手B获胜664场,占比66.4%

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
lca18214474709 + 1 + 1 我很赞同!
ee789852 + 1 + 1 吾爱破解论坛有你更精彩!

查看全部评分

我心飞翔1995 发表于 2021-8-7 00:36
你要发报错才好排查,python的报错还是容易看的
Lv归来 发表于 2021-8-7 01:59
Wapj_Wolf 发表于 2021-8-7 07:40
偶也是小白,很容易犯语法错误的。
luanshils 发表于 2021-8-7 09:17
排版下次可以把布局分成,第一部分总代码,第二部分输出结果,第三部分文字说明。有相应的代码包围标签。或者用MD写
 楼主| wuxi 发表于 2021-8-7 09:27
ShiratoriAira 发表于 2021-8-7 01:22
测试环境:Python 3.8

如果是Python 2.x,第一行加上[mw_shl_code=python,true]from __future__ import  ...

谢谢大佬们!!
Idongt 发表于 2021-8-7 11:37
有错误 也看不出来 尴尬
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 17:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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