r/pythonhelp • • Nov 25 '21

I am having trouble with this code:

import random

random number pick

npx = random.randint(-9,9) npy = random.randint(9,18)

random

rx = random.randint(-9,9) ry = random.randint(9,18)

bot

x = -9 y = 18

system

while True:

#chexk if y is less thn x
if npy >= npx:
    npy = random.randint(0,18)
elif ry >= x:
    ry = random.randint(0,18)    
elif y >= x:
    y = random.randint(0,18)
    
#random number is found and another number is picked  
r = random.randint(rx,ry)
number_pick = random.randint(npx,npy)

#bot guesses the number    
bot = random.randint(x,y)

#print
print('    for R the number was',r)
print('    for NP was',number_pick)
print('    bot guess',bot)
print('')

#check if bot was correct
if bot == r and bot == number_pick:
    print('        corect guess')
    break
if bot != r  and bot != number_pick:
    print('        incorect')
    chooice = random.randint(0,4)
    
    #randomizing the numbers
    if chooice == 0:
        npy -= 1
        ry -= 1
        y -= 1
    elif chooice == 1:
        npy += 1
        ry += 1
        y += 1
    elif chooice == 2:
        npx -= 1
        rx -= 1
        x -= 1
    elif chooice == 3:
         npy += 1
         ry += 1
         x += 1
    else:
        pass
 
#endintion              
print('')

It's giving this error:

Traceback (most recent call last): File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module> start(fakepyfile,mainpyfile) File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start exec(open(mainpyfile).read(), main.dict) File "<string>", line 22, in <module> File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/random.py", line 316, in randrange raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width)) ValueError: empty range for randrange() (8, 5, -3)

[Program finished]

2 Upvotes

6 comments sorted by

View all comments

1

u/[deleted] Nov 25 '21

You need to make sure that you do not call randrange with arguments that will produce an empty range. For example, if rx and ry are the same value.