Need help with python random import

Question:
I’m having trouble using the random import inside pygame to create a random x and y coordinate for a sprite to generate at.

The error I keep getting is:
File “main.py”, line 114, in
char.update()
File “main.py”, line 73, in update
coin.create_new()
File “main.py”, line 93, in create_new
coor_y = random.randint(450, 328)
File “/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/random.py”, line 370, in randint
return self.randrange(a, b+1)
File “/nix/store/9kzgdmf4q5vxshisils7pzgc6342pcsy-python3-3.10.0/lib/python3.10/random.py”, line 353, in randrange
raise ValueError(“empty range for randrange() (%d, %d, %d)” % (istart, istop, width))
ValueError: empty range for randrange() (450, 329, -121)

Repl link:
https://replit.com/@BraedynFrench2/GET-THE-BTTB?v=1

code snippet:
class Coin(pygame.sprite.Sprite):
  def __init__(self):
    super().__init__()
    self.surf = pygame.transform.scale(pygame.image.load("bttb.jpeg"), (30,30))
    self.rect = self.surf.get_rect(center = (coor_x, coor_y))
    
  def create_new(self):
    coor_x = random.randint(15, 1220) 
    coor_y = random.randint(450, 328)
    coin_count = coin_count + 1
    print("Coins = ", coin_count)

I don’t remember exactly, but you may need to have the range numbers in order from smallest to largest. To do this, you can either manually sort the numbers or you can add a function that takes two numbers and returns them sorted in a list. This would require adding the sorting function to each randint function called.

2 Likes

Nice catch! I missed that actually. To solve this error, change:

coor_y = random.randint(450, 328)

to

coor_y = random.randint(328, 450)

On the subject of @sylashur’s function, you could simply have this:

import random
def safeRandint(low: int, high: int) -> int:
  """# Function: safeRandint

## Inputs:
  low - int
  high - int

## Returns:
  A random integer between high and low.

## Throws:
  None"""
  try:
    return random.randint(low, high)
  except:
    return random.randint(high, low)
2 Likes

I haven’t had much experience with try except but this seems like a great example application, you could even import randint as unsafeRandint and define randint as this function to save one ctrl-h.

They still have to change something, as their code calls “random.randint”. I do understand where you’re coming from though, so here you go:

from random import unsafeRandint
def randint(low: int, high: int) -> int:
  """# Function: randint (safe)

## Inputs:
  low - int
  high - int

## Returns:
  A random integer between high and low.

## Throws:
  None"""
  try:
    return unsafeRandint(low, high)
  except:
    return unsafeRandint(high, low)
1 Like

Thank you, yeah I usually import just randint so I didn’t pay attention to the actual code

Thanks for the help. Of course it’d be something as simple as a syntax error caused by mixing up the two numbers. I’m not too educated with programming as everything I’ve learned has been self-taught. So it’s mostly just trial and error but I couldn’t get this one down and I’m glad I got your help on this one. Thanks again :slight_smile:

No problem, I’m happy to help out, and I’ll even do things like that sometimes. Note that I wasn’t even the first one to notice it, and I only commented once someone else noticed it, because I didn’t see it at first.

Yeah I did notice someone else answer it but what they were saying was a bit more complex so it went over my head. Your’s made it much easier to understand and also eyecatching for my ADHD brain

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.