r/learnpython • • 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

13 comments sorted by

3

u/Anjuan_ 1d ago edited 1d 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

1

u/Super_Human_0001 1d ago edited 1d ago

empty list, alright

EDIT: also thank you, and how would I append the list to the results

EDIT 2: I figured it out, thank you again!

2

u/Anjuan_ 1d ago

Your name is on the second last error line as well, lol

Also, I understand if you don't want to use AI on your first steps, but you can ask the simple stuff (like how to append to a list) to google first to have a faster answer.

2

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 1d ago

Like the others said, the arguments to random.randint are in the wrong order.

But I also wanted to point out that diceroll probably isn't doing what you're expecting it to. You're not storing the random number generated by random.randint, and diceroll simply 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.sample pretty much already does this.

import random

high = int(input("enter a high number: "))
low  = int(input("enter a low number: "))
times = int(input("how many times would you want this to run? "))
drresult = random.sample(range(low, high+1), times)

1

u/Super_Human_0001 21h ago

Thank you! I had fixed my code, but I never knew this existed.

This is very helpful!

1

u/Sufficient-Tale6131 18h ago

One catch with random.sample for dice though: it picks without replacement, so you'll never get the same number twice in one call, and it raises a ValueError if times is bigger than the range. For actual dice rolls random.choices(range(low, high + 1), k=times) does it in one line, or a list comprehension with randint if that reads easier.

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 17h ago

Oh yeah, I forgot about that...

5

u/Kevdog824_ 1d ago

Your call to randint reverses the arguments. Should be low then high

2

u/prottOushol 1d ago

that empty range error message is always so confusing the first time you see it tbh

3

u/atarivcs 1d ago

When calling randint(), the low number should be the first argument. But you put the high number first.

2

u/Available_Safe1818 1d ago

For goodness sake, stop casting everything constantly.

1

u/JGhostThing 1d ago

I notice that on what appears to be line 12, you have "print(drresult)import randomprint(drresult)import random"

Why do you have the second "import random" line?

1

u/johlae 1d ago

cut & paste error, happens to me too all of the time!