代码有没有问题
import randomcounts = int(input("请输入抛硬币的次数:"))
if counts>100:
ignore=True
else:
ignore=False
heads = 0 #统计正面的次数
tails = 0 #统计反面的次数
i = 0
print("开始抛硬币实验:")
while i < counts:
num = random.randint(1,10)
if num % 2:
heads += 1
if not ignore:
print("正面",end=" ")
else:
tails += 1
if not ignore:
print("反面",end=" ")
i += 1
print("")
print("一共模拟了",counts,"次抛硬币,结果如下:")
print("正面:",heads,"次",sep="")
print("反面:",heads,"次",sep="")
代码如上,要实现的效果如下图,文件名为抛硬币。
但是我的代码执行之后正反面出现的次数为什么总是一样的,如下图抛硬币(我的作业) print("正面:",heads,"次",sep="")
print("反面:",heads,"次",sep="")
这两次不都打印了同一个变量?
修改为:
print("")
print("一共模拟了",counts,"次抛硬币,结果如下:")
print("正面:",heads,"次",sep="")
print("反面:",tails,"次",sep="") smallwh 发表于 2024-6-6 11:10
print("正面:",heads,"次",sep="")
print("反面:",heads,"次",sep="")
这两次不都打印了同一个变量?
好的,太感谢了,原来是非常低级的错误{:10_277:}
页:
[1]