School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,495 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,845 people online right now. Registration is fast and FREE... Join Now!




Python Battle Bot Program

 

Python Battle Bot Program, Counter not working

MGWalden

8 May, 2009 - 06:46 PM
Post #1

New D.I.C Head
*

Joined: 30 Apr, 2009
Posts: 9



Thanked: 2 times
My Contributions
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.

CODE

#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

User is offlineProfile CardPM
+Quote Post


Ed_Bighead

RE: Python Battle Bot Program

9 May, 2009 - 04:01 PM
Post #2

D.I.C Head
Group Icon

Joined: 26 Apr, 2009
Posts: 166



Thanked: 14 times
Dream Kudos: 50
My Contributions
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.
CODE
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

User is offlineProfile CardPM
+Quote Post

MGWalden

RE: Python Battle Bot Program

9 May, 2009 - 05:24 PM
Post #3

New D.I.C Head
*

Joined: 30 Apr, 2009
Posts: 9



Thanked: 2 times
My Contributions
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.

CODE

#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:

CODE

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
User is offlineProfile CardPM
+Quote Post

Ed_Bighead

RE: Python Battle Bot Program

9 May, 2009 - 07:31 PM
Post #4

D.I.C Head
Group Icon

Joined: 26 Apr, 2009
Posts: 166



Thanked: 14 times
Dream Kudos: 50
My Contributions
...You didn't revise the code I suggested, it still says ammo=kill() instead of just kill()...
User is offlineProfile CardPM
+Quote Post

paathos

RE: Python Battle Bot Program

13 May, 2009 - 10:47 AM
Post #5

New D.I.C Head
*

Joined: 2 Mar, 2009
Posts: 6



Thanked: 1 times
My Contributions
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()):
CODE

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'


User is offlineProfile CardPM
+Quote Post

perryboy

RE: Python Battle Bot Program

16 May, 2009 - 02:40 AM
Post #6

New D.I.C Head
*

Joined: 16 May, 2009
Posts: 1

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

python

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?')




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()


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.gif ***

This post has been edited by JackOfAllTrades: 16 May, 2009 - 07:01 AM
User is offlineProfile CardPM
+Quote Post

3liminat3r

RE: Python Battle Bot Program

22 May, 2009 - 06:01 PM
Post #7

D.I.C Head
**

Joined: 22 May, 2009
Posts: 50

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()
User is offlineProfile CardPM
+Quote Post

cembry90

RE: Python Battle Bot Program

23 May, 2009 - 01:39 PM
Post #8

New D.I.C Head
*

Joined: 9 May, 2009
Posts: 1

QUOTE(MGWalden @ 8 May, 2009 - 06: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.

CODE

#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: 161
User is offlineProfile CardPM
+Quote Post

cools325

RE: Python Battle Bot Program

20 Aug, 2009 - 12:02 PM
Post #9

New D.I.C Head
*

Joined: 20 Aug, 2009
Posts: 1

QUOTE(cembry90 @ 23 May, 2009 - 01:39 PM) *

QUOTE(MGWalden @ 8 May, 2009 - 06: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.

CODE

#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.
User is offlineProfile CardPM
+Quote Post

code_m

RE: Python Battle Bot Program

20 Aug, 2009 - 12:08 PM
Post #10

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 118



Thanked: 8 times
My Contributions
Note that you really should be counting down:

python

>>> 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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:49AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month