【Python基础】循环输出随机数例子:生存游戏
随机数小例子:生存游戏
返回: Python基础 索引页
设想有一只monster,开始后获得一些食物,在30天内,每天都随机获得一些十五,但是也每天随机消耗食物,如果某天食物不足以满足消耗,在monster死亡。如果熬过30天,则monster胜利。
import random; monster = 'Monster001'; current_food = random.randint(1,5); target_days = 10; counter = target_days; survive_hint = 0; while counter >=0: print (' '); print('Day:'+ str(target_days - counter +1)); counter = counter -1; print ('------------------------------------'); print (monster+' has: ' + str(current_food) + ' food'); world_product_food = random.randint(1,10); ## got random amount of food from outside world current_food= current_food+ world_product_food; print (monster + ' got :' + str(world_product_food) + ' foods from the world' ); daily_consume = random.randint(1, 8); ## consume random amount of food every day current_food= current_food - daily_consume; if (current_food > 0 ): print (monster + ' consumed: ' + str(daily_consume)); print (monster + ' has now: ' + str(current_food) +' food left'); else: print (monster + ' need to consum: '+ str(daily_consume) ); print (monster + ' has no enought food now'); print ('------------------------------------'); if(current_food > 0): print (monster + ' survived with food: ' + str(current_food)); else: print (monster + ' died because no enough food'); survive_hint = -1; break; if (counter == 0): break; ## if all the days passed, then monster win if (survive_hint == -1): print (' '); print ('##########################'); print (monster + ' did not succeeded'); else: print (' '); print ('##########################'); print (monster + ' succeeded for those ' + str(target_days) + ' days');
可能的输出是这样的:
Day:1 ------------------------------------ Monster001 has: 1 food Monster001 got :7 foods from the world Monster001 consumed: 3 Monster001 has now: 5 food left ------------------------------------ Monster001 survived with food: 5 Day:2 ------------------------------------ Monster001 has: 5 food Monster001 got :2 foods from the world Monster001 consumed: 4 Monster001 has now: 3 food left ------------------------------------ Monster001 survived with food: 3 Day:3 ------------------------------------ Monster001 has: 3 food Monster001 got :5 foods from the world Monster001 consumed: 6 Monster001 has now: 2 food left ------------------------------------ Monster001 survived with food: 2 Day:4 ------------------------------------ Monster001 has: 2 food Monster001 got :8 foods from the world Monster001 consumed: 2 Monster001 has now: 8 food left ------------------------------------ Monster001 survived with food: 8 Day:5 ------------------------------------ Monster001 has: 8 food Monster001 got :5 foods from the world Monster001 consumed: 3 Monster001 has now: 10 food left ------------------------------------ Monster001 survived with food: 10 Day:6 ------------------------------------ Monster001 has: 10 food Monster001 got :3 foods from the world Monster001 consumed: 1 Monster001 has now: 12 food left ------------------------------------ Monster001 survived with food: 12 Day:7 ------------------------------------ Monster001 has: 12 food Monster001 got :1 foods from the world Monster001 consumed: 3 Monster001 has now: 10 food left ------------------------------------ Monster001 survived with food: 10 Day:8 ------------------------------------ Monster001 has: 10 food Monster001 got :1 foods from the world Monster001 consumed: 1 Monster001 has now: 10 food left ------------------------------------ Monster001 survived with food: 10 Day:9 ------------------------------------ Monster001 has: 10 food Monster001 got :3 foods from the world Monster001 consumed: 7 Monster001 has now: 6 food left ------------------------------------ Monster001 survived with food: 6 Day:10 ------------------------------------ Monster001 has: 6 food Monster001 got :6 foods from the world Monster001 consumed: 5 Monster001 has now: 7 food left ------------------------------------ Monster001 survived with food: 7 ########################## Monster001 succeeded for those 10 days
也可能是这样的:
Day:1 ------------------------------------ Monster001 has: 2 food Monster001 got :8 foods from the world Monster001 consumed: 7 Monster001 has now: 3 food left ------------------------------------ Monster001 survived with food: 3 Day:2 ------------------------------------ Monster001 has: 3 food Monster001 got :1 foods from the world Monster001 need to consum: 6 Monster001 has no enought food now ------------------------------------ Monster001 died because no enough food ########################## Monster001 did not succeeded
返回: Python基础 索引页