random.randint生成错误的数字

时间:2018-01-14 10:51:02

标签: python random

所以我正在制作一个快速骰子滚轮(用于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! 到底是怎么回事?!

1 个答案:

答案 0 :(得分:0)

你的if语句不正确,它们应该是这样的:

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

你做的方式使得" 4"评估为True,因此if总是进入第一个语句,即D4。