Python 使用随机软件包时的空白文本

Python 使用随机软件包时的空白文本,python,random,Python,Random,我在做一个骰子程序,每当我试着输入“D4”(因为这是我现在唯一拥有的东西),我都会得到我编码的随机数,然后是空白文本 我正在使用: Windows 10主页 Python 3.6 这是我的代码: from random import * import os import time os.system('cls') print("What dice would you like to roll?") print("D4, D6, D10, D12, D20") D4 = int(randin

我在做一个骰子程序,每当我试着输入“D4”(因为这是我现在唯一拥有的东西),我都会得到我编码的随机数,然后是空白文本

我正在使用:
Windows 10主页
Python 3.6

这是我的代码:

from random import *
import os
import time
os.system('cls')

print("What dice would you like to roll?")

print("D4, D6, D10, D12, D20")

D4 = int(randint(1, 4))

D6 = int(randint(1, 6))

D10 = int(randint(1, 10))

D12 = int(randint(1, 12))

D20 = int(randint(1, 20))

age = int(D4)

diceI = input("Name: ")

if (diceI == D4):
    print("lol")

else:
    os.system('cls')
    time.sleep(.1)
    print("4")
    os.system('cls')
    time.sleep(.1)
    print("2")
    os.system('cls')
    time.sleep(.1)
    print("3")
    os.system('cls')
    time.sleep(.1)
    print("4")
    os.system('cls')
    time.sleep(.1)
    print("1")
    os.system('cls')
    time.sleep(.1)
    print("3")
    os.system('cls')
    time.sleep(.1)
    print("2")
    os.system('cls')
    time.sleep(.1)
    print(random.randint(1,4))
我知道这是一个非常混乱的代码,所以我将尝试解释为什么我有某些事情是这样的

首先,这是:

我这样做的原因是,如果我只是尝试在代码中使用“if”语句,那么什么都不会显示。这就是为什么我只是让它与else语句中的实际代码一起显示

其次,这:

我知道这很容易简化,但我想做的是在实际结果出来之前显示随机数,这样做的效果是,这实际上是一个你正在滚动的骰子


我真的希望有人看到这一点,因为我已经试着修复它大约5天了。

因此,如果我理解正确,这应该可以满足您的要求:

import os
import time
from random import *

os.system('cls')

print("What dice would you like to roll?")

print("D4, D6, D10, D12, D20")

D4 = randint(1, 4) # No need for the int conversions. randint returns an int (as the name suggests)

D6 = randint(1, 6)

D10 = randint(1, 10)

D12 = randint(1, 12)

D20 = randint(1, 20)

age = D4

diceI = input("Name: ")

if diceI == "D4": # if the input you gave equals the string "D4"
    os.system('cls')
    time.sleep(.1)
    print("4")
    os.system('cls')
    time.sleep(.1)
    print("2")
    os.system('cls')
    time.sleep(.1)
    print("3")
    os.system('cls')
    time.sleep(.1)
    print("4")
    os.system('cls')
    time.sleep(.1)
    print("1")
    os.system('cls')
    time.sleep(.1)
    print("3")
    os.system('cls')
    time.sleep(.1)
    print("2")
    os.system('cls')
    time.sleep(.1)
    print(randint(1, 4)) # No need for the random.randint, since you used import *
只是

而不是

if (diceI == D4):

,因为D4是一个字符串,所以您的错误来自将字符串dici=input(“Name:”)与存储在变量D4=int(randint(1,4))中的整数进行比较

diceI
是一个字符串,因此将其与另一个字符串进行比较,而不是整数。另外,
D4=randint(1,4)
已经足够了-randint已经生成了一个整数,您不需要将它与
int(randint(1,4))
转换为一

您可以直接从输入中获得骰子,并跳过部分代码,如下所示:

import random 
import os
import time
os.system('cls')

print("What dice would you like to roll?")
print("D4, D6, D10, D12, D20")

dice = ""
# continue to ask for input until the input is one of the ones in the list
while dice not in ["D4", "D6", "D10", "D12", "D20"]:
    dice = input("Name: ")

diceNr = int(dice[1:])  # remove the D and convert the remaining number to integer

if (diceNr == 4):
    print("lol") 
else:
    # get a random number between 1 and diceNr 
    print(random.randint(1,diceNr ))  

random.randint(a,b)

返回一个随机整数N,这样一个
骰子
就是一个字符串,所以将其与1比较,而不是与整数比较。另外,
randint'产生一个
int`-你不需要将它用
int(…)
转换成一个……你是什么意思?非常感谢你,我已经想了一段时间了。
import random 
import os
import time
os.system('cls')

print("What dice would you like to roll?")
print("D4, D6, D10, D12, D20")

dice = ""
# continue to ask for input until the input is one of the ones in the list
while dice not in ["D4", "D6", "D10", "D12", "D20"]:
    dice = input("Name: ")

diceNr = int(dice[1:])  # remove the D and convert the remaining number to integer

if (diceNr == 4):
    print("lol") 
else:
    # get a random number between 1 and diceNr 
    print(random.randint(1,diceNr ))