求大神看看哪里错了,openai非说没错,但是结果根本不对
import randomnumber = int(input("请输入抛硬币次数"))
tail = 0
head = 0
ctail,chead,mhead,mtail,_ = 0, 0, 0,0,0
for i in range(number):
x = random.randint(1,2)
if x == 1:
if number <= 100:
print("正", end = " ")
head += 1
if _ != 2:
chead += 1
if chead > mhead:
mhead = chead
else:
chead = 0
_ = 1
else:
if number <= 100:
print("反", end = " ")
tail += 1
if _ != 1:
ctail += 1
if ctail > mtail:
mtail = ctail
else:
ctail = 0
_ = 2
print(f"正面出现{head}次, 反面出现{tail}次,最多连续正面{mhead}次,最多连续反面{mtail}次") 看着不舒服,用代码格式发一下再注释说明一下ctail,chead,mtail,mhead,_的意思可能有人会来帮你
如果我对你的代码理解有误请指出
import random
number = int(input("请输入抛硬币次数"))
tail = 0
head = 0
# ctail,chead:本次连续次数
# mtail,mhead:最大连续次数
#_:状态码
ctail,chead,mhead,mtail,_ = 0, 0, 0,0,0
for i in range(number):
x = random.randint(1,2)
if x == 1:
if number <= 100:
print("正", end = " ")
head += 1
if _ != 2:
chead += 1
if chead > mhead:
mhead = chead
else:
chead = 0
_ = 1
else:
if number <= 100:
print("反", end = " ")
tail += 1
if _ != 1:
ctail += 1
if ctail > mtail:
mtail = ctail
else:
ctail = 0
_ = 2
print(f"正面出现{head}次, 反面出现{tail}次,最多连续正面{mhead}次,最多连续反面{mtail}次")
import random
head , tail , ctail , chead , mhead , mtail , n = 0 , 0 , 0 , 0 , 0 , 0 , 0
number = int(input("请输入抛硬币次数 : "))
for i in range(number):
x = random . randint(1 , 2)
if x != n:
if x == 1:
if tail > mtail : mtail = tail
tail = 0
else:
if head > mhead : mhead = head
head = 0
if x == 1:
chead , head = chead + 1 ,head + 1
else:
ctail , tail = ctail + 1 , tail + 1
if number <= 100:
print("正", end = " ") if x == 1 else print("反", end = " ")
n = x
print()
print(f"正面出现{chead}次, 反面出现{ctail}次,最多连续正面{mhead}次,最多连续反面{mtail}次")
页:
[1]