2013-11-02 43 views
0

當我使用randint()有時會出現這樣的錯誤:randint()錯誤「空範圍randrange」蟒蛇隨機模塊中

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

這是爲什麼/我如何阻止它的發生?

即使我可以告訴,這個錯誤dosnt apppear任何特定的randit()調用,只會偶爾出現。

誤差帶我到實際的隨機模塊:

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

這裏是我的整個程序(我稱之爲randint()常)

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() 
+3

請顯示產生此錯誤的代碼 – TerryA

+2

[隨機模塊不工作的可能的重複。 ValueError:randrange()(1,1,0)的空範圍](http://stackoverflow.com/questions/4513818/random-module-not-working-valueerror-empty-range-for-randrange-1-1 -0) – jwodder

+1

最有可能的原因是有時候你叫'randint(1,0)'。如果是這樣,停止這樣做;-) –

回答

4

randint(A,B)應該叫其中b和b應該大於a的論點a和b。否則()由randint稱爲randrange()方法將引發一個值誤差這樣的:

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

檢查這個源與randint()(line:237) and randrange() (line:173)方法更好地理解隨機模塊。

+0

在我所有的randint()調用中,B都大於A.就像我所說的那樣,這隻會偶爾出現,而不會出現在特定的場合。 – Thedudxo