骰子游戏不会打破循环

2024-09-27 01:24:19 发布

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

一个循环不断地用==或is函数中断,即使它应该继续,所以玩家可以再次尝试滚动。我不明白为什么有人能帮我。那太好了。你知道吗

elif roll1 is roll2:
        print(score1)
        continue

或者

elif roll1 == roll2:
        print(score1)
        continue

他们两个都不工作,所以没人知道为什么。你知道吗

 while True:
    print(" ")
    input("Press enter to roll player 1 ")
    print("Rolling dice!")
    roll1=random.randint(1,6)
    roll2=random.randint(1,6)
    print(roll1)
    print(roll2)
    total=(roll1 + roll2)
    print("Your total is:" ,total)
    score1=score1 +total
    even = [2,4,6,8,10,12]
    odd = [3,5,7,9,11]

    if total in even:
        score1=score1 +10
        print(score1)
        break
    elif total in odd:
        score1=score1 -5
        print(score1)
        break
    elif roll1 == roll2:
        print(score1)
        continue

有完整的循环


Tags: inisrandomtotalevenoddprintrandint
1条回答
网友
1楼 · 发布于 2024-09-27 01:24:19

问题在于:

if total in even:
    score1=score1 +10
    print(score1)
    break
elif total in odd:
    score1=score1 -5
    print(score1)
    break
elif roll1 == roll2:
    print(score1)
    continue

您永远不会到达最后一个elif,因为total总是在evenodd中。如果您想先重新滚动,您应该将elif上移到如下位置:

if roll1 == roll2:
    print(score1)
    continue
elif total in odd:
    score1=score1 -5
    print(score1)
    break
elif total in even:
    score1=score1 +10
    print(score1)
    break

相关问题 更多 >

    热门问题