9 Replies - 11447 Views - Last Post: 20 August 2009 - 01:08 PM Rate Topic: -----

#4 MGWalden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 30-April 09

Re: Python Battle Bot Program

Post icon  Posted 08 May 2009 - 07:46 PM

OK, on to my next challenge. The counter for the number of shots fired is not working. Any idea what is wrong? It is supposed to fire up to 5 shots then say it is empty.

#battle bot program
#
#
def main():
 endProgram ='no'
 ammo=int(0)
 while endProgram == 'no':
	fuel = 200
	fuel = float (fuel)
	print

	#display menu
	print 'Menu Selections: '
	print '1 - Fire Weapon '
	print '2 - Move Forward '
	print '3 - Move Backward '
	print '4 - Exit '

	#check menu selection
	choice = input ('Enter your selection ')
	choice = float (choice)
	if choice == 1:
		print ammo
		ammo=ammo+1
		print ammo
		if ammo <= 5:
			ammo=kill()
		elif ammo > 5:
			print 'You are out of ammunition'
			print ammo
			return ammo
	elif choice == 2:
		move = moveBot(fuel)
	elif choice == 3:
		move = moveBot(fuel)
	elif choice == 4:
		Exit = end()
	elif choice >= 5:
		print 'Not a valid action '
		print 'Please try again '
	else:
		endProgram = raw_input ('Do you want to end program?')
	('Enter yes or no):')
 while not (endProgram == 'yes' or endprogram == 'no'):
		print 'Please enter a yes or no '
		endProgram == raw_input ('Do you want to end program?')

#fire weapion 5 shots only
def fire(ammo):
	print ammo
	ammo = ammo + 1
	print ammo
	if ammo <= 5:
		fire = kill(ammo)
	elif ammo > 5:
		print 'You are out of ammunition! '
		return
	else:
		return

#move bot
def moveBot(fuel):
	fuel = input ('How far would you like to move? ')
	

#kill enemy
def kill():
	print 
	print 'How far away is the enemy? '
	killDis = input ('Enter distance in feet now! ')
	#check effect
	if killDis <= 20:
		print 'Enemy has been destroyed! '
		return
	elif killDis <= 40:
		print 'Enemy has been partially disabled. '
		return
	elif killDis >= 41:
		print 'The enemy has been unharmed! '
		return

#end program
def end():
	endProgram = raw_input ('Do you want to end program? yes or no ')

#calls main
main()



Thanks again,

Michael Walden
mgwalden@gmail.com
Was This Post Helpful? 1

#5 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Python Battle Bot Program

Posted 09 May 2009 - 05:01 PM

ammo=kill()
kill() doesn't return anything, but you're setting ammo equal to the return of kill(), so ammo becomes 'NoneType'. Just call kill() by istelf and you're fine.
 if choice == 1:
		print ammo
		ammo=ammo+1
		print ammo
		if ammo <= 5:
			kill()
		elif ammo > 5:
			print 'You are out of ammunition'
			print ammo
			return ammo

Was This Post Helpful? 0
  • +
  • -

#6 MGWalden  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 30-April 09

Re: Python Battle Bot Program

Posted 09 May 2009 - 06:24 PM

OK, I must have missed something. When I run the code it works fine the first time I fire but gives the following error. First is the revised program, then the run results.

#battle bot program
#
#
def main():
 endProgram ='no'
 ammo=int(0)
 while endProgram == 'no':
	fuel = 200
	fuel = float (fuel)
	print

	#display menu
	print 'Menu Selections: '
	print '1 - Fire Weapon '
	print '2 - Move Forward '
	print '3 - Move Backward '
	print '4 - Exit '

	#check menu selection
	choice = input ('Enter your selection ')
	choice = float (choice)
	if choice == 1:
		print ammo
		ammo=ammo+1
		print ammo
		if ammo <= 5:
			ammo=kill()
		elif ammo > 5:
			print 'You are out of ammunition'
			print ammo
			return ammo
	elif choice == 2:
		move = moveBot(fuel)
	elif choice == 3:
		move = moveBot(fuel)
	elif choice == 4:
		Exit = end()
	elif choice >= 5:
		print 'Not a valid action '
		print 'Please try again '
	else:
		endProgram = raw_input ('Do you want to end program?')
	('Enter yes or no):')
 while not (endProgram == 'yes' or endprogram == 'no'):
		print 'Please enter a yes or no '
		endProgram == raw_input ('Do you want to end program?')

#fire weapion 5 shots only
def fire(ammo):
	if choice == 1:
		print ammo
		ammo=ammo+1
		print ammo
		if ammo <= 5:
			kill()
		elif ammo > 5:
			print 'You are out of ammunition'
			print ammo
			return ammo

#move bot
def moveBot(fuel):
	fuel = input ('How far would you like to move? ')
	

#kill enemy
def kill():
	print 
	print 'How far away is the enemy? '
	killDis = input ('Enter distance in feet now! ')
	#check effect
	if killDis <= 20:
		print 'Enemy has been destroyed! '
		return
	elif killDis <= 40:
		print 'Enemy has been partially disabled. '
		return
	elif killDis >= 41:
		print 'The enemy has been unharmed! '
		return

#end program
def end():
	endProgram = raw_input ('Do you want to end program? yes or no ')

#calls main
main()



Run Results:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
	
IDLE 1.2.2	  
>>> ================================ RESTART ================================
>>> 

Menu Selections: 
1 - Fire Weapon 
2 - Move Forward 
3 - Move Backward 
4 - Exit 
Enter your selection 2
How far would you like to move? 2

Menu Selections: 
1 - Fire Weapon 
2 - Move Forward 
3 - Move Backward 
4 - Exit 
Enter your selection 1
0
1

How far away is the enemy? 
Enter distance in feet now! 4
Enemy has been destroyed! 

Menu Selections: 
1 - Fire Weapon 
2 - Move Forward 
3 - Move Backward 
4 - Exit 
Enter your selection 3
How far would you like to move? 4

Menu Selections: 
1 - Fire Weapon 
2 - Move Forward 
3 - Move Backward 
4 - Exit 
Enter your selection 1
None

Traceback (most recent call last):
  File "C:/Documents and Settings/Michael/My Documents/IT Ref/IT104/Battle Botb.py", line 87, in <module>
	main()
  File "C:/Documents and Settings/Michael/My Documents/IT Ref/IT104/Battle Botb.py", line 24, in main
	ammo=ammo+1
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
>>> 



All hints are welcome.

Michael Walden
Was This Post Helpful? 0
  • +
  • -

#7 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Python Battle Bot Program

Posted 09 May 2009 - 08:31 PM

...You didn't revise the code I suggested, it still says ammo=kill() instead of just kill()...
Was This Post Helpful? 0
  • +
  • -

#8 paathos  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 02-March 09

Re: Python Battle Bot Program

Posted 13 May 2009 - 11:47 AM

OK, you have a number of problems here. Don't be insulted, you are learning, and this is a good learning opportunity.
1 - Don't cast 0 as an int, it already is
2 - Don't cast 200 to a float, just declare it as 200.0 and it will be
3 - Don't cast choice to a float, you are testing on ints
4 - If the choice is 1, then call fire(ammo), don't insert the code here
5 - Don't count up to 5, this isn't very intuitive. Set ammo to 5 and count down every time you fire.
6 - Use booleans to test on when you can
7 - If user didn't enter 1-4, it's wrong then, so just use "else"

Here are some changes I would make (just for main() and fire()):
def main():
	endProgram = False
	ammo = 5
	fuel = 200.0
	while not endProgram:
				
		#display menu
		print 'Menu Selections: '
		print '1 - Fire Weapon '
		print '2 - Move Forward '
		print '3 - Move Backward '
		print '4 - Exit '

		#check menu selection
		choice = input('Enter your selection ')
		if choice == 1:
			fire(ammo)
		elif choice == 2:
			move = moveBot(fuel)
		elif choice == 3:
			move = moveBot(fuel)
		elif choice == 4:
			ex = raw_input('Do you really want to quit? ')
				if ex in ['Y','y','YES','Yes','yes']:
					endProgram = True
		else:
			print 'Not a valid action '
			print 'Please try again '

#fire weapion 5 shots only
def fire(ammo):
	if ammo > 0:
		kill()
		ammo -= 1
	else:
		print 'You are out of ammunition'


Was This Post Helpful? 0
  • +
  • -

#9 perryboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-May 09

Re: Python Battle Bot Program

Posted 16 May 2009 - 03:40 AM

okei..i have the same program working on...i've got the fire ammo part working and it works great with the same code you have...but im having problem with the fuel...i cant seem to get my bot stop when it reaches that 200.0 fuel limit..do i have to float (fuel) and added to fuel = 0 ? help please...tnx

def main():
    endProgram = 'no'
    ammo=int(0)
    while endProgram == 'no':
        fuel = 200.0
        fuel = float (fuel)
        print
        


    #Display Menu

        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:
            print ammo
            ammo = ammo + 1
            print ammo
            if ammo <= 5:
                kill()
            elif ammo >= 5:
                print 'You are out of ammunition'
                print ammo
                return ammo
        elif choice == 2:
            move = moveBot(fuel)
        elif choice == 3:
            move = moveBot(fuel)
        elif choice == 4:
            Exit = end()
        elif choice >= 5:
            print 'Not a valid action'
            print 'Please try again'
        else:
            endProgram = raw_input('Do you want to end program?')
        ('Enter yes or no):')
    while not (endProgram == 'yes' or endProgram == 'no'):
        print 'Please enter yes or no'
        endProgram == raw_input('Do you want to end program?')




[color="#FF0000"]def moveBot(fuel):
    fuel = input('How far would you like to move? ')
    print fuel
    fuel = fuel + float(fuel)
    if fuel <= 200.0:
        'no'
    elif fuel >= 200.0:
        print 'You are out of fuel'
        return
main()[/color]

def kill():
    print
    print'How far is the enemy? '
    killDis = input('Enter distance in feet now! ')
    if killDis <= 20:
        print 'Enemy has been destroyed!'
        return
    elif killDis <= 40:
        print 'Enemy has been partially disabled. '
        return
    elif killDis >= 41:
        print 'The enemy has been unharmed! '
        return


main()



def fire(ammo):
    print ammo
    ammo = ammo + 1
    print ammo
    if ammo <= 5:
        fire = kill(ammo)
    elif ammo >= 5:
        print 'You are out of ammunition'
        return

main()
    

def end():
    endProgram = raw_input('Do you want to end program? yes or no? ')

main()



*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 16 May 2009 - 08:01 AM

Was This Post Helpful? 0
  • +
  • -

#10 3liminat3r  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 22-May 09

Re: Python Battle Bot Program

Posted 22 May 2009 - 07:01 PM

Here how i made my battle bot. Try to look at yours then mine to see what ya did wrong. I'm new to programming and this page helped me out alot...

def main():

ammo = 5
fuel = 200.0
option = 0
distance = 0
feet = 0

while option !=4:
option = choice()
if option == 1:
kill()
ammo=ammo-1
if ammo<1:
print 'Out of ammo'
exit()



elif option == 2:
fuel =move(fuel)

if fuel < 0:
print "\n"*30
print ' You have ran out of fuel'
exit()
elif option == 3:
fuel = move(fuel)
if fuel < 0:
print "\n"*30
print ' You have ran out of fuel'
exit()
elif option == 4:
exit()


def choice():
print "\n"*30
print '1: Fire a weapon '
print '2: Move Foward '
print '3: Move Backward '
print '4: Exit '
option = input('Enter your choice: ')
return option




def kill():

distance = input('Enter how far you would like to fire in feet!')
if (distance <= 20):
print
print 'opponent is destroyed'

elif (distance <= 40):
print
print ' HIT, battlebot has partially been disabled'

elif (distance >= 41):
print "\n"*5
print ' Battlebot is unharmed '


return





def move(fuel):

feet = input('How far do you want to go in feet? ')

if feet >= 200:
print ' You have went off the map! Please try again.'
elif feet >= 100:
print ' You have move 50 feet but a landmind holds you from going any furthor.'
elif feet >= 50:
print ' The battle bot has been block by a landmind.'
elif feet <= 20:
print ' You have moved ' , feet ,'feet.'

fuel-=feet

return fuel





main()
Was This Post Helpful? 0
  • +
  • -

#11 cembry90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-May 09

Re: Python Battle Bot Program

Posted 23 May 2009 - 02:39 PM

View PostMGWalden, on 8 May, 2009 - 06:46 PM, said:

OK, on to my next challenge. The counter for the number of shots fired is not working. Any idea what is wrong? It is supposed to fire up to 5 shots then say it is empty.

#battle bot program
#
#
def main():
 endProgram ='no'
 ammo=int(0)
 while endProgram == 'no':
	fuel = 200
	fuel = float (fuel)
	print

	#display menu
	print 'Menu Selections: '
	print '1 - Fire Weapon '
	print '2 - Move Forward '
	print '3 - Move Backward '
	print '4 - Exit '

	#check menu selection
	choice = input ('Enter your selection ')
	choice = float (choice)
	if choice == 1:
		print ammo
		ammo=ammo+1
		print ammo
		if ammo <= 5:
			ammo=kill()
		elif ammo > 5:
			print 'You are out of ammunition'
			print ammo
			return ammo
	elif choice == 2:
		move = moveBot(fuel)
	elif choice == 3:
		move = moveBot(fuel)
	elif choice == 4:
		Exit = end()
	elif choice >= 5:
		print 'Not a valid action '
		print 'Please try again '
	else:
		endProgram = raw_input ('Do you want to end program?')
	('Enter yes or no):')
 while not (endProgram == 'yes' or endprogram == 'no'):
		print 'Please enter a yes or no '
		endProgram == raw_input ('Do you want to end program?')

#fire weapion 5 shots only
def fire(ammo):
	print ammo
	ammo = ammo + 1
	print ammo
	if ammo <= 5:
		fire = kill(ammo)
	elif ammo > 5:
		print 'You are out of ammunition! '
		return
	else:
		return

#move bot
def moveBot(fuel):
	fuel = input ('How far would you like to move? ')
	

#kill enemy
def kill():
	print 
	print 'How far away is the enemy? '
	killDis = input ('Enter distance in feet now! ')
	#check effect
	if killDis <= 20:
		print 'Enemy has been destroyed! '
		return
	elif killDis <= 40:
		print 'Enemy has been partially disabled. '
		return
	elif killDis >= 41:
		print 'The enemy has been unharmed! '
		return

#end program
def end():
	endProgram = raw_input ('Do you want to end program? yes or no ')

#calls main
main()



Thanks again,

Michael Walden
mgwalden@gmail.com


Hey buddy. Saw your post about 2 or 3 weeks ago. Funny thing is my college just fuinished this. I have attached the perfect, 100% working, version. The password is 'battle' (no quotes). Have fun!

~Chris

Attached File(s)

  • Attached File  bbot.zip (1.12K)
    Number of downloads: 1008

Was This Post Helpful? 0
  • +
  • -

#12 cools325  Icon User is offline

  • New D.I.C Head

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

Re: Python Battle Bot Program

Posted 20 August 2009 - 01:02 PM

View Postcembry90, on 23 May, 2009 - 01:39 PM, said:

View PostMGWalden, on 8 May, 2009 - 06:46 PM, said:

OK, on to my next challenge. The counter for the number of shots fired is not working. Any idea what is wrong? It is supposed to fire up to 5 shots then say it is empty.

#battle bot program
#
#
def main():
 endProgram ='no'
 ammo=int(0)
 while endProgram == 'no':
	fuel = 200
	fuel = float (fuel)
	print

	#display menu
	print 'Menu Selections: '
	print '1 - Fire Weapon '
	print '2 - Move Forward '
	print '3 - Move Backward '
	print '4 - Exit '

	#check menu selection
	choice = input ('Enter your selection ')
	choice = float (choice)
	if choice == 1:
		print ammo
		ammo=ammo+1
		print ammo
		if ammo <= 5:
			ammo=kill()
		elif ammo > 5:
			print 'You are out of ammunition'
			print ammo
			return ammo
	elif choice == 2:
		move = moveBot(fuel)
	elif choice == 3:
		move = moveBot(fuel)
	elif choice == 4:
		Exit = end()
	elif choice >= 5:
		print 'Not a valid action '
		print 'Please try again '
	else:
		endProgram = raw_input ('Do you want to end program?')
	('Enter yes or no):')
 while not (endProgram == 'yes' or endprogram == 'no'):
		print 'Please enter a yes or no '
		endProgram == raw_input ('Do you want to end program?')

#fire weapion 5 shots only
def fire(ammo):
	print ammo
	ammo = ammo + 1
	print ammo
	if ammo <= 5:
		fire = kill(ammo)
	elif ammo > 5:
		print 'You are out of ammunition! '
		return
	else:
		return

#move bot
def moveBot(fuel):
	fuel = input ('How far would you like to move? ')
	

#kill enemy
def kill():
	print 
	print 'How far away is the enemy? '
	killDis = input ('Enter distance in feet now! ')
	#check effect
	if killDis <= 20:
		print 'Enemy has been destroyed! '
		return
	elif killDis <= 40:
		print 'Enemy has been partially disabled. '
		return
	elif killDis >= 41:
		print 'The enemy has been unharmed! '
		return

#end program
def end():
	endProgram = raw_input ('Do you want to end program? yes or no ')

#calls main
main()



Thanks again,

Michael Walden
mgwalden@gmail.com


Hey buddy. Saw your post about 2 or 3 weeks ago. Funny thing is my college just fuinished this. I have attached the perfect, 100% working, version. The password is 'battle' (no quotes). Have fun!

~Chris


Hello, I have also been assigned this project at my college and since you seem to have completed it i was wondering how i would go about writing the pseudocode for this using the python code.
Was This Post Helpful? 0
  • +
  • -

#13 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 202
  • Joined: 21-April 09

Re: Python Battle Bot Program

Posted 20 August 2009 - 01:08 PM

Note that you really should be counting down:

>>> ammo = 5
>>> if choice == 1:
...     ammo -= 1



It would probably suit you better to write this in your independant function.

Also, if you would like to see another example program, see my webpage: http://theradclan.com/code_m
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1