|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
counts = int(input("请输入抛硬币的次数:"))
i = 0
n = 0
m = 0
c = 0
b = 2
d = 0
print("开始抛硬币实验:")
if counts < 100:
while i < counts :
num = random.randint(1,10)
a = num
if num % 2 :
print("正面", end=" ")
n = n + 1
else:
print("反面", end=" ")
m = m + 1
if b % 2 and a % 2 and num % 2 == 0:
b = a
c = c + 1
else:
if c > d:
d = c
c = 0
i += 1
print("一共模拟了",counts,"次抛硬币,结果如下:",sep = "")
print("正面:", n ,"次" , sep = "" )
print("反面:", m ,"次" , sep = "")
print("最多连续正面:", d ,"次",sep = "")
这个代码是计算硬币最多连续正面的次数,想问一下我这里哪里出问题了吗,运行之后d的值永远是0
import random
counts = int(input("请输入抛硬币的次数:"))
i = 0
n = 0
m = 0
c = 0
b = 2
d = 0
print("开始抛硬币实验:")
if counts < 100:
while i < counts :
num = random.randint(1,10)
a = num
if num % 2 :
print("正面", end=" ")
n = n + 1
else:
print("反面", end=" ")
m = m + 1
if b % 2 and a % 2 and num % 2 == 0:
b = a
c = c + 1
else:
if c > d:
d = c
c = 0
print(a, b, c, d) # <---------- 多加这一行你就明白了
i += 1
print("一共模拟了",counts,"次抛硬币,结果如下:",sep = "")
print("正面:", n ,"次" , sep = "" )
print("反面:", m ,"次" , sep = "")
print("最多连续正面:", d ,"次",sep = "")
|
|