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.
JavaScript
x
20
20
1
import keyboard #Import keyboard stuff like enter (pip install keyboard)
2
import random #Import random stuff
3
import time
4
5
sides = 1
6
sidesSelect = input("Amount of sides the dice has. If empty, 6: ")
7
is_non_empty= bool(sidesSelect)
8
if is_non_empty is False:
9
sides = 6
10
else:
11
sides = sidesSelect
12
13
time.sleep(0.5)
14
15
while True:
16
nmb = random.randint(1,sides) #Get random integer
17
print("The dice rolled ", nmb)
18
input('Press enter to roll the dice again') #Ask if you want to throw again
19
time.sleep(random.uniform(0.2,0.8))
20
I already tried changing == is, and nothing happened
JavaScript
1
2
1
if is_non_empty is false:
2
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.
JavaScript
1
6
1
#python3
2
3
sidesSelect = int(input("Amount of sides the dice has. If empty, 6: ") or "6")
4
5
nmb = random.randint(1, sidesSelect) #Get random integer
6