使用不同的模块+包装和计数

发布于 2025-01-21 19:58:39 字数 1434 浏览 1 评论 0原文

是的。

我需要定义两个变量:

throw_yut1(),它们分别随机选择'배'或'등',分别为60%和40%。

打印4的throw_yut4()来自throw_yut1(),例如'배등배배'或'배배배배'等

import random

random.seed(10)

def throw_yut1():
    if random.random() <= 0.6 :
        return '배'
    else:
        return '등'
        

def throw_yut4():
    result = ''

    for i in range(4):
        result = result + throw_yut1()
    
    return result

。main.py是以下内容。

在这里,我需要重复throw_yut4 1000次,并打印获取if语句中列出的每个变量的值和百分比。

import yut

counts = {}

for i in range(1000):
   
    result = yut.throw_yut4

    back = yut.throw_yut4().count('등') 
    belly = yut.throw_yut4().count('배')

    if back == 3 and belly == 1:
        counts['도'] = counts.get('도', 0) + 1
    elif back == 2 and belly == 2:
        counts['개'] = counts.get('개', 0) + 1
    elif back == 1 and belly == 3:
        counts['걸'] = counts.get('걸', 0) + 1
    elif back == 0 and belly == 4:
        counts['윷'] = counts.get('윷', 0) + 1
    elif back == 4 and belly == 0:
        counts['모'] = counts.get('모', 0) + 1


for key in ['도','개','걸','윷','모']:
    print(f'{key} - {counts[key]} ({counts[key] / 1000 * 100:.1f}%)')

我不断得到

도 - 33 (3.3%)
개 - 115 (11.5%)
걸 - 131 (13.1%)
윷 - 22 (2.2%)
모 - 1 (0.1%)

,但我打算得到

도 - 157 (15.7%)
개 - 333 (33.3%)
걸 - 349 (34.9%)
윷 - 135 (13.5%)
모 - 26 (2.6%)

如何解决错误?

yut.py is the following.

I need to define two variables:

throw_yut1() which randomly selects '배' or '등' by 60% and 40% respectively.

throw_yut4() which prints 4 results from throw_yut1() such as '배등배배' or '배배배배' etc.

import random

random.seed(10)

def throw_yut1():
    if random.random() <= 0.6 :
        return '배'
    else:
        return '등'
        

def throw_yut4():
    result = ''

    for i in range(4):
        result = result + throw_yut1()
    
    return result

main.py is the following.

Here, I need to repeat throw_yut4 1000 times and print the value and percentages of getting each variables that are listed in if statement.

import yut

counts = {}

for i in range(1000):
   
    result = yut.throw_yut4

    back = yut.throw_yut4().count('등') 
    belly = yut.throw_yut4().count('배')

    if back == 3 and belly == 1:
        counts['도'] = counts.get('도', 0) + 1
    elif back == 2 and belly == 2:
        counts['개'] = counts.get('개', 0) + 1
    elif back == 1 and belly == 3:
        counts['걸'] = counts.get('걸', 0) + 1
    elif back == 0 and belly == 4:
        counts['윷'] = counts.get('윷', 0) + 1
    elif back == 4 and belly == 0:
        counts['모'] = counts.get('모', 0) + 1


for key in ['도','개','걸','윷','모']:
    print(f'{key} - {counts[key]} ({counts[key] / 1000 * 100:.1f}%)')

I keep getting

도 - 33 (3.3%)
개 - 115 (11.5%)
걸 - 131 (13.1%)
윷 - 22 (2.2%)
모 - 1 (0.1%)

but I am meant to get

도 - 157 (15.7%)
개 - 333 (33.3%)
걸 - 349 (34.9%)
윷 - 135 (13.5%)
모 - 26 (2.6%)

How can I fix my error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2025-01-28 19:58:39

我认为问题在于代码的这一部分:

for i in range(1000):
    result = yut.throw_yut4

    back = yut.throw_yut4().count('등') 
    belly = yut.throw_yut4().count('배')

似乎应该是:

for i in range(1000):
    result = yut.throw_yut4()

    back = result.count('등') 
    belly = result.count('배')

否则您将从独立yut.throw_yut4()呼叫中倒数/腹部,因此某些未手持(可能是意外)的结果是可能像背部= 4 and belly = 4 ...这就是为什么您计算的总数小于1000和100%

I think the problem is this part of the code:

for i in range(1000):
    result = yut.throw_yut4

    back = yut.throw_yut4().count('등') 
    belly = yut.throw_yut4().count('배')

Seems like it should be:

for i in range(1000):
    result = yut.throw_yut4()

    back = result.count('등') 
    belly = result.count('배')

Otherwise you are counting back/belly from independent yut.throw_yut4() calls, so some unhandled (and presumably unintended) results are possible like back=4 and belly=4 ... this is why the totals you count are less than 1000 and 100%

往事随风而去 2025-01-28 19:58:39

看来您的计数字典并没有填充1000个元素。这可能是因为您正在调用yut.throw_yut4()两次,这给出了不同的结果,因此您的条件检查都没有通过。

反试试:

result = yut.throw_yut4()
back = result.count('등')
belly = result.count('등')

천만에요

It seems like your counts dictionary isn't getting filled with 1000 elements. It is likely because you are calling the yut.throw_yut4() twice, which give different results, such that none of your conditional checks pass.

try this instead:

result = yut.throw_yut4()
back = result.count('등')
belly = result.count('등')

천만에요

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文