random.randint生成错误的数字

2024-05-19 15:39:42 发布

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

所以我做了一个快速骰子辊(D&;D) 当我试着掷骰子时,它只会产生从1到4的数字

import random


def d4():
    x = (random.randint(1,4))
    print(x)

def d6():
    x = (random.randint(1,6))
    print(x)

def d8():
    x = (random.randint(1,8))
    print(x)

def d10():
    x = (random.randint(1,10))
    print(x)

def d12():
    x = (random.randint(1,12))
    print(x)

def d20():
    x = (random.randint(1,20))
    print(x)

def d100():
    x = (random.randint(1,100))
    print(x)


choice = input("What dice will you roll: ")


if choice == "d4" or "4":
    d4()
elif choice == "d6" or "6":
    d6()
elif choice == "d8" or "8":
    d8()
elif choice == "d10" or "10":
    d10()
elif choice == "d12" or "12":
    d12()
elif choice == "d20" or "20":
    d20()
elif choice == "d100" or "100":
    d100()
else:
    print(random.randint(1,20))

例如,当我把我的输入作为d100,我只能得到1,2,3,或4! 到底怎么回事


Tags: ordefrandom骰子printrandintchoiceelif
1条回答
网友
1楼 · 发布于 2024-05-19 15:39:42

如果您的陈述不正确,应如下所示:

if choice == "d4" or choice == "4":
    d4()

您这样做的方式使“4”的值为True,因此if总是进入第一个语句,即D4

相关问题 更多 >