当我使用 randint() 时,有时会出现此错误:
ValueError: empty range for randrange() (1,1, 0)
为什么会这样/我该如何阻止它发生?
据我所知,我没有做错任何事情,这个错误不会出现在任何特定的 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()