r/learnpython • u/Super_Human_0001 • 1h ago
Python Dice Roller Help
Hello, I was making a sort of dice roller function that could be called to make a range of random numbers. However, I get an error and cant understand really what is wrong.
My Code to do this is:
import random
def diceroll(high, low, times):
for _ in range(int(times)):
random.randint(int(high), int(low))
return high, low, times
high = input("enter a high number: ")
low = input("enter a low number: ")
times = input("how many times would you want this to run? ")
drresult = diceroll(high=int(high), low=int(low), times=int(times))
print(drresult)import random
def diceroll(high, low, times):
for _ in range(int(times)):
random.randint(int(high), int(low))
return high, low, times
high = input("enter a high number: ")
low = input("enter a low number: ")
times = input("how many times would you want this to run? ")
drresult = diceroll(high=int(high), low=int(low), times=int(times))
print(drresult)And the Error:
File "D:\PYTHONPROJECTS\EXERCISESANDTESTS\TEST0001\TESTAPP-0001.py", line 12, in <module>
drresult = diceroll(high=int(high), low=int(low), times=int(times))
File "D:\PYTHONPROJECTS\EXERCISESANDTESTS\TEST0001\TESTAPP-0001.py", line 6, in diceroll
random.randint(int(high), int(low))
File "C:\Users\Ryan Axtell\AppData\Local\Programs\Python\Python310\lib\random.py", line 370, in randint
return self.randrange(a, b+1)
File "C:\Users\-\AppData\Local\Programs\Python\Python310\lib\random.py", line 353, in randrange
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (20, 2, -18)Any help would be appreciated!
2
Upvotes
2
2
u/atarivcs 1h ago
When calling randint(), the low number should be the first argument. But you put the high number first.
•
u/Expensive-Bear-1376 1m ago
Haven't read this yet but already upvoted because you formatted the code and showed the error. (Kinda sad that this is so rare that it makes me happy to see that done for once.)
•
u/Anjuan_ 57m ago edited 53m ago
In random.randint the first number is the low one and the second is the high one. Also, I think you're returning the wrong thing. Your code just rolls the dice "times" times but does nothing with the results and returns its inputs. If you want to take a list of integers from it, you should initialize an empty list and append the dicerolls to it.
EDIT: Also, please edit your post so that your name doesn't show on the filepaths, there are some weird people on the internet you never know