使用不同的模块+包装和计数
是的。
我需要定义两个变量:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于代码的这一部分:
似乎应该是:
否则您将从独立
yut.throw_yut4()
呼叫中倒数/腹部,因此某些未手持(可能是意外)的结果是可能像背部= 4 and belly = 4 ...这就是为什么您计算的总数小于1000和100%I think the problem is this part of the code:
Seems like it should be:
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%看来您的计数字典并没有填充1000个元素。这可能是因为您正在调用
yut.throw_yut4()
两次,这给出了不同的结果,因此您的条件检查都没有通过。反试试:
천만에요
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:
천만에요