I was trying to make a simple dice in py, and I tried to make so you can change the amount of sides the dice has and if it’s left empty, to default to 6. But when I input something, it crashes.
import keyboard #Import keyboard stuff like enter (pip install keyboard)
import random #Import random stuff
import time
sides = 1
sidesSelect = input("Amount of sides the dice has. If empty, 6: ")
is_non_empty= bool(sidesSelect)
if is_non_empty is False:
sides = 6
else:
sides = sidesSelect
time.sleep(0.5)
while True:
nmb = random.randint(1,sides) #Get random integer
print("The dice rolled ", nmb)
input('Press enter to roll the dice again') #Ask if you want to throw again
time.sleep(random.uniform(0.2,0.8))
I already tried changing == is, and nothing happened
if is_non_empty is false:
Advertisement
Answer
One issue your code has is it is trying to use the input directly without taking care of the type.
input() returns a string, so it has to be converted to proper type before using it in randint
Try something like this.
#python3
sidesSelect = int(input("Amount of sides the dice has. If empty, 6: ") or "6")
nmb = random.randint(1, sidesSelect) #Get random integer