hi, so while I was working my way through learn python the hard way, I was struck with inspiration to try and write a little script just to see what I could do. But I ended up using a huge amount of if statements, some of which check for up to four conditions before exicuting as they should. Is there any way to do this following piece of code more efficiently? I couldn't find anything, so wonder if you have any opinions.
def best_function():
gold=0
production=0
spearmen=0
print "there are enemy warriors closing in! You have %d gold, %d production and %d spearmen with you." %(gold, production, spearmen)
print "You are given the choice of work grassland tile, build monument, found new city, or recruit spearman."
print "Founding the city will cost you 50 gold and 6 production. Recruiting a spearman will cost you 25 and 1 production, while the monument will cost you a further 25 and 4 production. The grassland tile will give you 10 gold every time it is worked, as well as 2 production."
print "You can also fight. But just one spearman won't cut it!"
built_monument=False
built_city=False
while True:
choice=raw_input("What will you do? ")
if choice=="work grassland tile":
print "You soon reap the benefits of your hard work."
production+=2
gold+=20
print "You now have %d gold and %d production." %(gold, production)
elif choice=="found new city" and gold >=50 and production >=6:
print "Your glorious city is founded and ready to help you conquer the world!"
built_city=True
gold-=50
production-=6
print "You now have %d gold and %d production." %(gold, production)
elif choice =="build monument" and not built_city:
print "You can't do that yet, because you need a city to coordinate the building operations!"
elif choice=="build monument" and built_city and gold >=25 and production >=4:
print "Your shiny new monument stands prowd, honouring your warriors and boosting their morale. Get enough spearmen to take the fight to the enemy!"
built_monument=True
gold-=25
production-=2
print "You now have %d gold and %d production." %(gold, production)
elif choice=="build monument" and built_city and gold <=25 or production <=2:
print "You do not have enough resources to perform this action!"
elif choice=="recruit spearman" and not built_city:
print "You can't do that yet, because you need a home base for your spearmen to stay at!"
elif choice=="recruit spearman" and built_city and not built_monument and gold >=25 and production >=1:
print "You are able to find an able boddied man to carry the spear, but he is easily demoralized and dies within a week of service. Thus, your resources have been wasted. Find a way to boost morale!"
gold-=25
production-=1
print "You now have %d gold and %d production." %(gold, production)
elif choice=="recruit spearman" and built_city and gold <=25 or production <=1:
print "You do not have enough resources to propperly equip your spearman!"
elif choice=="recruit spearman" and built_city and built_monument and gold >=25 and production >=1:
print "Your loyal spearman is trained and ready for battle!"
spearmen+=1
gold-=25
production-=1
print "You now have %d gold, %d production and %d spearmen." %(gold, production, spearmen)
elif choice=="fight" and spearmen <5:
dead("Your pittyful force is easily slaughtered by the greedy hordes. Your lands are pillaged, your homes destroyed, your men sold as slaves, your women raped, your... I think you get the idea.")
elif choice=="fight" and spearmen >=5:
print "Your mighty army of unshakeable warriors easily slaughters the enemie's pittyful force! Their lands are pillaged, their homes destroyed, their men sold as slaves, their women raped, their... I think you get the idea. Did you really think your nation would treat the other nation differently just because you're in control? Wrong! Still, congradulations."
exit(0)
elif choice=="found new city" and gold <50 or production <6:
dead("Your glorious city falls short and cannot protect itself against the barbarian hordes.")
else:
dead("You dodder around until the warriors find you and tare you to pieces.")
def dead(why):
print why, "Good job!"
exit(0)
Pages 1