Pythong Program Help

Backwards module not working in program!

Page 1 of 1

4 Replies - 692 Views - Last Post: 21 August 2010 - 02:49 PM Rate Topic: -----

#1 Guest_Scott*


Reputation:

Pythong Program Help

Posted 21 August 2010 - 07:40 AM

I am writing this program for a group project at college, I seem to have everything working, however I can not get my Move Backward command to work when the user inputs to move backward it just returns blank! Any help would be greatly appreciated!!!

def main():
###FUEL VARIABLES
    fused = 0
    fuel = int(200)
    feet = int(0)
###AMMO VARIABLES
    global ammo
    ammo = int(5)
    global used
    used = int(0)
###MOVE VARIABLES
    block = ''
    dist = 0
    move = int(0)
###OTHER VARIABLES
    choice = int(0)
    endProg = 'no'
    print 'Choose your Battle Mode!: '
    print '1 - Fire Weapon '
    print '2 - Move Forward '
    print '3 - Move Backward '
    print '4 - Exit '
    while endProg == 'no':
        if ammo < 0:
            ammo = int(0)
        if used > 5:
            used = int(5)
        print fuel,'fuel,',fused,'used.'
        print ammo,'ammo,',used,'used.'
###MENUCHECKS
        choice = raw_input('Enter your selection:')
        if choice == '1' or choice == '2' or choice == '3' or choice == '4':
            if int(choice) == 1:
                fire(ammo)
                ammo = ammo - 1
                used = used + 1
            elif int(choice) == 2 or choice == 3:
                if choice == 2:
                    dir = 'backward.'
                else: 
                    dir = 'forward.'
                print('Are there any objects in the way?')
                block = raw_input('Y or N:')
                if block == 'Y' or block == 'y':
                   objLoc = int(input('Enter the distance, in feet, the object is:'))
                   feet = input('How far do you want to move?')
                   if feet <= objLoc:
                       if not feet > fuel:
                           fuel = fuel - feet
                           fused = fused + feet
                       else:
                           print 'Not enough fuel to move that far.'
                   else:
                      print 'There is an object in the way.'
                elif block == 'N' or block == 'n':
                   feet = input('How far do you want to move?')
                   if not feet > fuel:
                       fuel = fuel - feet
                       fused = fused + feet
                       print 'Successfully moved',feet,'feet',dir
                   else:
                       print 'Not enough fuel to move that far.'
                else:
                   print 'Error: Invalid selection.'
            elif int(choice) == 4:
                print 'Goodbye!'
                exitProg()
        else:
            print 'Error: Invalid selection.'
###FIRE&AMMO
def fire(ammo):
    if ammo > 0:
        dist = raw_input('Enter the number, in feet, the enemy is:')
        if dist <= '20':
            print 'The enemy has been VAPORIZED!!.'
            return
        elif dist <= '40':
            print 'The enemy took partial damage.'
            return
        else:
            print 'MISFIRE! RELOAD!!! RELOAD!!!!.'
            return
    elif ammo <= 0:
        print('Out of ammo!')
###EXIT
def exitProg():
    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()



Is This A Good Question/Topic? 0

Replies To: Pythong Program Help

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Pythong Program Help

Posted 21 August 2010 - 08:57 AM

maybe you should use int() when you compare to 3 as you do when you compare to 2 on line 37?
Was This Post Helpful? 0
  • +
  • -

#3 codemonkey27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-August 10

Re: Pythong Program Help

Posted 21 August 2010 - 09:08 AM

Like this? If so it still does not return anything when the user puts in option 3?

   if int(choice) == 1:
                fire(ammo)
                ammo = ammo - 1
                used = used + 1
            elif int(choice) == 2 or choice == 3:
                if int(choice) == 2:
                    dir = 'backward.'
                else: 
                    dir = 'forward.'


Was This Post Helpful? 0
  • +
  • -

#4 codemonkey27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-August 10

Re: Pythong Program Help

Posted 21 August 2010 - 09:14 AM

moopet, thank you for your help I put int in and it worked perfectly!!
Was This Post Helpful? 0
  • +
  • -

#5 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: Pythong Program Help

Posted 21 August 2010 - 02:49 PM

I found an other problem in your program: In the fire(ammo) function. What happens if the user input for dist is say "155" ?
Then the condition of the first if clause tests "155"<="20". But strings are ordered lexicographically, so "155"<"20". Certainly not what you want.

Instead of comparing strings
dist = raw_input('Enter the number, in feet, the enemy is:')
if dist <= '20': ...


you might want to convert the input to an integer and then compare it to an integer:
dist = int(raw_input('Enter the number, in feet, the enemy is:'))
if dist <= 20: ...


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1