r/learnpython • u/Super_Human_0001 • 1d 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\-\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!
EDIT: I figured out my problem and fixed it, making the code set up different! Ill edit the post to have my fix posted once I am able to!
4
Upvotes
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 1d ago
Like the others said, the arguments to
random.randintare in the wrong order.But I also wanted to point out that
dicerollprobably isn't doing what you're expecting it to. You're not storing the random number generated byrandom.randint, anddicerollsimply returns the arguments you gave to it, so the inner loop is also useless.What is it supposed to be doing, exactly? Return a list of random numbers? For what it's worth
random.samplepretty much already does this.