Python100掷硬币

2024-09-28 03:19:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要编写代码,将模拟100投币和efter,告诉我们有多少尾巴和头部,我们下降。我做错什么了?你知道吗

import random

orzel = 0
reszka = 0
suma = orzel + reszka
while suma != 100:
    rzut = random.randint(1,6)
    if rzut == 1:
        orzel +=1
    if rzut == 2:
        reszka +=1

print(orzel, "i", reszka)
input("koniec")

Tags: 代码importifrandom尾巴printsumarandint
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:37

您可以通过使用random.randint(1,2)并在循环内移动suma = orzel + reszka来修复代码,但最好使用类似这样的python方法:

from random import choice
outcome = {'orzel':0, 'reszka':0}
for i in range(100):
    outcome[choice(['orzel','reszka'])] += 1
print(outcome)

相关问题 更多 >

    热门问题