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

2

u/Goobyalus Nov 25 '21

Please format your code properly by adding 4 spaces before each line of code and leaving a blank line before and after the code block

1

u/Dazzling_Guard_5191 Nov 25 '21 edited Nov 25 '21

Oh. The format looks off because of the '#' and Reddit text competition

<Edit> I just realized what I just sent and text competition is not the problem. I will go through my code and properly space things out.

1

u/xelf Nov 25 '21

Select all your code and indent it 4 extra spaces, you can use the <> button or the codeblock button.

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('')

1

u/Dazzling_Guard_5191 Nov 25 '21

That's interesting! Why/how would it change the outcome of the code if I indent it 4 extra spaces?

2

u/xelf Nov 25 '21

It'll format correctly on reddit and not look like a mess. =)

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.