1

when I use randint() sometimes this error will appear:

ValueError: empty range for randrange() (1,1, 0)

why is this/how do i stop it from happening?

im not doing anything wrong as far as i can tell, this error dosnt apppear on any specific randit() call and only appears every now and then.

the error takes me to the actual random module:

raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)

Here is my whole program (i call randint() often)

    from random import randint

units = 5
food = 20
days_without_food = 0
SP = 4
print '------------------------------------------------------'
print 'welcome! starting a new game'
print '------------------------------------------------------'
location = 'plains'

def search(): #do2 create leadership skill or search skill
    global units
    global food
    global SP
    if SP > 0:
        SP = SP - 1
        found = randint(1,3)
        if found == 1 or found == 2:
            if units == 0:
                units = randint(1,5)
                print '------------------------------------------------------'
                print 'you recruited %s units!' %units
                main()
            a = units / 2
            ammount = randint(1,a)
            units = units + ammount
            print '------------------------------------------------------'
            print 'you recruted %s units!' %ammount #recruit a random ammount of units not over half ammount of already owned.
            main()
        else:
            print '------------------------------------------------------'
            print 'you did not find anything'
            main()
    else:
        print '------------------------------------------------------'
        print 'your army is too tired to look for anything'
        main()



def battle(): #do1 create villages with a random amount of units between 100-300
    global SP
    global units
    global food
    if SP == 0:
        print '------------------------------------------------------'
        print 'your army is too tired to fight'
        main()
    else:
        SP = SP -1
    if units == 0:
        print '------------------------------------------------------'
        print 'it is madness to go looking for a fight without an army'
        main()
    a = units / 2
    b = units * 2
    e_units = randint(a,b)
    e_units = int(e_units) #random ammount of enemy units reletave to  ammount of player units
    if e_units == 0:
        e_units = randint(1,3)

    guess = randint(e_units-5,e_units+5)
    if guess < 0:
        guess = randint(1,e_units+10)
    print '------------------------------------------------------'
    print 'it looks like the enemy has %s units' %guess #unacurate guess of how many enemy units there are
    print 'a: attack'
    print 'b: leave'
    action = raw_input('a or b?')

    if action == 'a':
        units = units - e_units
        if units <0:
            units = 0
            print '------------------------------------------------------'
            print 'you lost the battle and all of your units'
            main()
        else:
            a = e_units * 2
            ammount = randint(1,a)
            food = food + ammount
            print '------------------------------------------------------'
            print 'you won the battle and found %s food!' %ammount + '   after the battle you have %s units' %units
            main() # battle

    else:
        chance = randint(1,10)
        if chance == 10:
            units = units - e_units
            if units <0:
                units = 0 #attempt to leve
            print '------------------------------------------------------'
            print 'it is to late, they spotted you!' + '   after the battle you have %s units' %units
            main()
        else:
            print '------------------------------------------------------'
            print 'you leave without the enemy spotting you'
            main()

def change_location():
    global food
    global units
    global SP
    global days_without_food
    if food < 0:
        days_without_food = days_without_food + 1
        if days_without_food > 0 and days_without_food < 3:
            chance = randint (1,5)
            if not chance == 5:
                print '------------------------------------------------------'
                print 'you have no food, but your men are stll loyal to you'
                food = 0
                main()
            elif chance == 5:
                deserters = randint(1,int(units/4))
                units = units - deserters
                print '------------------------------------------------------'
                print 'without any food, %d men deserted your army' % deserters
                food = 0
                main()
        else:
            chance = randint(1,2)
            if chance == 1:
                men_starved = randint(1,int(units/4))
                units = units - men_starved
                print '------------------------------------------------------'
                print 'without any food, %d units starved to death' % men_starved
                food = 0
                main()
            else:
                deserters = randint(1,int(units/4))
                units = units - deserters
                print '------------------------------------------------------'
                print 'without any food, %d men deserted your army' % deserters
                food = 0
                main()
    days_without_food = 0
    SP = 3
    food = food - int(units / 2)
    places = ['plains','beach','river','mountain','sea','desert','hills',
    'forest','volcano','jungle','storm','village'] #do3 add more places
    location = places[randint(0,len(places)-1)]
    main()


def main(): #do1 Create nomadic and settled versions
    global units
    global location
    global food
    global SP
    print '------------------------------------------------------'
    print 'you are at a %s' %location
    print 'a:look for a fight'
    print 'b:look for supplies'
    print 'c:Take stock'
    print 'd:Move on'
    action = raw_input('a,b,c, or d?')
    if action == 'a':
        battle()
    elif action == 'b':
        search()
    elif action == 'c':
        print '------------------------------------------------------'
        print 'food: %s.  ' %food + ' units: %s.  ' %units + ' Search points %s.  ' %SP
        main()
    elif action == 'd':
        change_location()
    else:
        main()

main()
4
  • 4
    Please show the code that produces this error Commented Nov 2, 2013 at 3:02
  • 3
    possible duplicate of Random module not working. ValueError: empty range for randrange() (1,1, 0) Commented Nov 2, 2013 at 3:12
  • 2
    Most likely cause is that sometimes you're calling randint(1, 0). If so, stop doing that ;-) Commented Nov 2, 2013 at 3:15
  • I have shown my full code. im not calling randint(a,b) where b is bigger than a. it is not a duplicate. Commented Nov 5, 2013 at 5:40

1 Answer 1

4

randint(a, b) should be called with the arguments a and b in which b should be greater than a. Else the randrange() method called by randint() will raise a value error like this:

raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)

Check this source for random module with randint()(line:237) and randrange() (line:173) methods for better understanding.

Sign up to request clarification or add additional context in comments.

1 Comment

B is greater than A on all of my randint() calls. like i said, this only appears sometimes and never on one specific ocassion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.