Battle Bot Issue

Everything runs till the end when i try to exit.

Page 1 of 1

1 Replies - 4165 Views - Last Post: 14 August 2009 - 03:18 PM Rate Topic: -----

#1 Cursecoin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 09

Battle Bot Issue

Posted 14 August 2009 - 03:13 PM

#08/10/09
#This program will provide control
#functions for a virtual Battlebot

#Final Lab - Programming a Robot Battlebot

def main():
	endProgram = False
	ammunition = 5
	fuel = 200.0
	totalFeet = 0
	enemyDistance = 200
	
	while not endProgram:
		print 'Menu Selections: '
		print '1 - Fire Weapon '
		print '2 - Move Forward '
		print '3 - Move Backward '
		print '4 - Exit '


		choice = input('Enter your selection ')
		if choice == 1:
			ammunition = fire(ammunition)
		elif choice == 2:
			fuel = moveFor(fuel)
		elif choice == 3:
			moveBackward = moveBack(fuel)
		elif int(choice) == 4:
			print 'Goodbye!'
			endProgram()
		else:
			print 'Error: Invalid selection.'

def fire(ammunition):
	if ammunition >= 1:
		fire = attack()
	else:
		print 'You are out of ammunition!'
	ammunition = ammunition - 1
	print ammunition
	return ammunition

def moveFor(fuel):
	print fuel
	moveFor = input ('How far would you like to move?')
	if moveFor <= fuel:
		fuel = fuel - moveFor
	elif fuel == 0:
		print 'You have no fuel left!'
	elif moveFor > fuel :
		print 'You need more fuel to move!'
	print fuel
	return fuel

def moveBack(fuel):
	print fuel
	moveBack = input ('How far would you like to move?')
	if moveBack <= fuel:
		fuel = fuel - moveBack
	elif fuel == 0:
		print 'You have no fuel left!'
	elif moveBack > fuel:
		print 'You need more fuel to move!'
	print fuel
	return fuel

def fuelLeft(fuel):
	fuelLeft = (fuel - moveFor or fuel - moveBack)
	
def attack():
	print 'How far away is the enemy? '
	attackDis = input ('Enter distance in feet. ')
	
	if attackDis <= 10:
		print 'Enemy has been destroyed! '
		return
	elif attackDis <= 25:
		print 'Enemy has been partially disabled. '
		return
	elif attackDis >= 26:
		print 'The enemy has been unharmed! '
		return

def endProgram():
	print 'Y or N:'
	true = raw_input('Do you wish to exit this program?')
	if true == 'y' or true == 'Y':
		quit()
	elif true == 'n' or true == 'N':
		print 'Welcome back.'
		return
	else:
		print 'Error: Invalid selection.'

#calls main
main()



I can't get the endProgram to run!

Is This A Good Question/Topic? 0
  • +

Replies To: Battle Bot Issue

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: Battle Bot Issue

Posted 14 August 2009 - 03:18 PM

The problem is that you have a boolean names endProgram and a function with the same name (that isn't allowed).

Try this:
#08/10/09
#This program will provide control
#functions for a virtual Battlebot

#Final Lab - Programming a Robot Battlebot

def main():
	endProgramBool = False
	ammunition = 5
	fuel = 200.0
	totalFeet = 0
	enemyDistance = 200
	
	while not endProgramBool:
		print 'Menu Selections: '
		print '1 - Fire Weapon '
		print '2 - Move Forward '
		print '3 - Move Backward '
		print '4 - Exit '


		choice = input('Enter your selection ')
		if choice == 1:
			ammunition = fire(ammunition)
		elif choice == 2:
			fuel = moveFor(fuel)
		elif choice == 3:
			moveBackward = moveBack(fuel)
		elif int(choice) == 4:
			print 'Goodbye!'
			endProgram()
		else:
			print 'Error: Invalid selection.'

def fire(ammunition):
	if ammunition >= 1:
		fire = attack()
	else:
		print 'You are out of ammunition!'
	ammunition = ammunition - 1
	print ammunition
	return ammunition

def moveFor(fuel):
	print fuel
	moveFor = input ('How far would you like to move?')
	if moveFor <= fuel:
		fuel = fuel - moveFor
	elif fuel == 0:
		print 'You have no fuel left!'
	elif moveFor > fuel :
		print 'You need more fuel to move!'
	print fuel
	return fuel

def moveBack(fuel):
	print fuel
	moveBack = input ('How far would you like to move?')
	if moveBack <= fuel:
		fuel = fuel - moveBack
	elif fuel == 0:
		print 'You have no fuel left!'
	elif moveBack > fuel:
		print 'You need more fuel to move!'
	print fuel
	return fuel

def fuelLeft(fuel):
	fuelLeft = (fuel - moveFor or fuel - moveBack)
	
def attack():
	print 'How far away is the enemy? '
	attackDis = input ('Enter distance in feet. ')
	
	if attackDis <= 10:
		print 'Enemy has been destroyed! '
		return
	elif attackDis <= 25:
		print 'Enemy has been partially disabled. '
		return
	elif attackDis >= 26:
		print 'The enemy has been unharmed! '
		return

def endProgram():
	print 'Y or N:'
	true = raw_input('Do you wish to exit this program?')
	if true == 'y' or true == 'Y':
		quit()
	elif true == 'n' or true == 'N':
		print 'Welcome back.'
		return
	else:
		print 'Error: Invalid selection.'

#calls main
main()



It works for me.

Hope that helps.
Was This Post Helpful? 1

Page 1 of 1