I have to make this program track how much fuel I have used. I have tried and tried, but I can't get it to work... I'm not at all looking for a solution... I just need a nudge/hint into the right direction.

def main():
    choice = 0
    ammo = 5
    fuel = 200

    while choice != 4 and ammo != 0:

        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:
            fireWeapon()
            
            ammo = ammo - 1
            if ammo == 0:
                print "You are out of ammo!"
            else:
                print "You have",ammo,"shots left"

        elif choice == 2:
            moveForward(fuel)
            
        elif choice == 3:
            moveBackward(fuel)

        elif choice == 4:
            print "Goodbye!"

        else:
            print "Invalid Input! Try Again."

def fireWeapon():
    distance = input("How far away is your target?")

    if distance <= 20:
        print "Target destroyed!"
    elif distance < 40:
        print "The target is partially disabled"
    else:
        print "Target is unharmed"

def moveForward(fuel):
    path = input("How many feet would you like to move backward?")
    obstacle = input("How many feet away is your nearest obstacle?")
    
    if path - obstacle == path:
        print "You have moved forward",path,"feet."

    if path - obstacle < path:
        distance = path - obstacle
        print "Your path is blocked. You can only move",path,"feet"
        
def moveBackward(fuel):
    path = input("How many feet would you like to move backward?")
    obstacle = input("How many feet away is your nearest obstacle?")
    
    if path - obstacle == path:
        print "You have moved forward",path,"feet."

    if path - obstacle < path:
        distance = path - obstacle
        print "Your path is blocked. You can only move",path,"feet"

main()

Recommended Answers

All 6 Replies

well first of all what is going wrong when you try to run it?

and have you tried something like:

def moveForward(fuel):
    path = input("How many feet would you like to move FOREward?")
    obstacle = input("How many feet away is your nearest obstacle?")

    if path - obstacle == path:
        print "You have moved forward",path,"feet."
        fuel-= path
        print('You have', fuel,'units of fuel left')
        return fuel

for each section or possibility?

as this is your first post I should remind you it's polite, but not required to reply with your solution or any further questions. To hit "mark thread solved" for people with similar problems in the future. And, no one minds up voting if you find it appropriate, happy coding!

in case you aren't familiar with -= it is the equivalent of saying var = var - int

Just so you know, I'm also a beginner in python, and programming in general. So I used your assignment to help me with what I'm learning, Classes and methods (non-private for now). I played around a little with the idea of that and here's what I have now.

class Starship(object):
    hp=100
    fuel=100
    ammo=100
    traveled=0
    killed=0

    def __init__(self, name):
        self.name=name
        print('\nAttention Captain of Starship', self.name,'\nit\'s time to make a move!')
    def fireWeapon(self, Enemy):
        try:
            distance=int(input('\nHow far is your target?: '))
            if distance < 10:
                Enemy.hurt(50)
            elif distance > 10 and distance < 51 :
                Enemy.hurt(30)
            elif distance > 50 and distance < 76:
                Enemy.hurt(15)
            else:
                print('Lasers will not do damage at that range')
            Starship.ammo-= 1
            print('\n',Starship.ammo,'percent power left in the lasers')
            print('\nShields at',Starship.hp,'percent captain!')
        except:
            print('Invalid Entry')

    def move(self):
        try:
            lyrs=int(input('How many units would you like to travel, 1-100?: '))
            import random
            obstacle= random.randint(1,101)
            if obstacle != lyrs:
                print('Moved', lyrs,'lightyears')
            else:
                print('There is an enemy in your way!')
                Decisions.__init__()
                
            Starship.fuel-= lyrs//10
            Starship.traveled += lyrs
            return Starship.traveled, Starship.fuel
        except:
           print('\n Invalid Selection')


class Enemy(object):
    enemyhp=100
    damage1=0

    def __init__(self):
        print('I am the enemy!')
        
    def lifeCheck():
        if Enemy.enemyhp< 1:
            print('\n\nEnemy annihalated!!!\n')
            Starship.killed += 1
        else:
            print('\nEnemy shields at', Enemy.enemyhp, 'percent captain!\n')
            print('\nThe enemy is firing back!')
            import random
            chance=random.randint(1,3)
            if chance==1:
                print('\n That was close but the enemy missed!')
            else:
                import random
                attackdamage=random.randint(1,51)
                Starship.hp-=attackdamage
                print('Shit! They got us captain!')
        return Enemy.enemyhp, Starship.killed


    def hurt(damage):
        Enemy.damage1 += damage
        Enemy.enemyhp=Enemy.enemyhp-Enemy.damage1
        Enemy.lifeCheck()

    def location():
        import random
        location=random.randint(1,101)

class Decisions(object):
    def __init__(self):
        choice=input('\nenter your decisions\n1. Attack\n2. Flee\nChoice: ')
        if choice == 1:
            Starship.fireWeapon(Enemy)
        else:
            import random
            success=random.randint(1,3)
            if success== 1:
                print('\nYou turned the ship around only to have all systems fail and be blasted out of the sky!\nYou have been punished for your cowardice!')
            else:
                Starship.move()







def main():
    global Enterprise
    Enterprise= Starship('Enterprise')
    Enterprise.fireWeapon(Enemy)

while Enemy.damage1 < 100:
    main()
print('Way to go captain!')
if Enemy.enemyhp < 0:
    Enemy.enemyhp=0
print(Enemy.enemyhp, Enemy.damage1)

while Starship.traveled < 100:
    Enterprise.move()
    print('\n', Enterprise.fuel, 'percent capability for the warp reactor captain')
commented: Only little bit to go: http://en.wikipedia.org/wiki/Star_Trek_%28text_game%29 +13

@pyguy62: unfortunately you had much to correct here my fast work over of your code, basically you are continueing to change the classes instead of having instances. Also you mix too much your output inside the program logic.

from __future__ import print_function
import random

try:
    input = raw_input
    print('Welcome Python2 player')
except:
    print('Welcome Python3 player')

class Starship(object):
    def __init__(self, name):
        self.name=name
        self.shields=100
        self.fuel=100
        self.ammo=100
        self.traveled=0
        print("""
Attention Captain of Starship %s
It's time to make a move!""" % self.name)
        self.enemy = Enemy(self)
        
    def fire_weapon(self):
        try:
            distance=int(input('\nHow far is your target?: '))
            if distance < 10:
                self.enemy.hurt(50)
            elif distance > 10 and distance < 51 :
                self.enemy.hurt(30)
            elif distance > 50 and distance < 76:
                self.enemy.hurt(15)
            else:
                print('Lasers will not do damage at that range')
                
            self.ammo -= 1
        except ValueError:
            print("\nSorry, sir, but ship's computer does not understand the command!")
            print("Try again, sir!\n")

    def move(self):
        try:
            lyrs=int(input('How far you want to move,sir? 1-100, please: '))
            obstacle= random.randint(1,101)
            if obstacle != lyrs:
                print('Moved', lyrs,'lightyears')
            else:
                print('There is an enemy in your way!')
                decisions(self)
                
            self.fuel -= lyrs//10
            self.traveled += lyrs
        except ValueError:
           print("\nSorry, sir, but computer needs number 1-100!\n")

    def status(self):
        print("""
%s percent power left in the lasers
Shields at %i percent captain!
We have fuel left %i.
Enemy shields are at %i!
"""% (self.ammo, self.shields, self.fuel, self.enemy.shields))
        
        return  self.shields > 0  and self.enemy.alive

class Enemy(object):

    def __init__(self, ship):
        self.shields = 100
        self.alive = True
        print('Enemy ready to fight!')
        self.ship = ship
        
    def hurt(self, damage):
        self.shields -= damage
        if self.shields < 1:
            self.alive = False
        else:
            if random.randint(1,3) > 1:
                self.ship.shields -= random.randint(1,51)
        self.alive = self.shields > 0

    def location(self):
        self.location=random.randint(1,101)

def desicions(ship):
    choice=input("""
Enter your decisions
    1. Attack
    2. Flee

    Choice: """)
    if choice == '1':
        ship.fire_weapon()
    elif choice == '2':
        success=random.randint(1,3)
        if success== 1:
            print("""
You turned the ship around only to have all systems fail and be blasted out of the sky!
You have been punished for your cowardice!""")
            ship.shields = -100
        else:
            ship.move()
    else:
        print('\nInvalid decision code, captain!')

def main():
    enterprise= Starship('Enterprise NX-1')

    while enterprise.status():
        desicions(enterprise)
        
    if enterprise.shields <= 0:
        print('We are dead! Game over!')
    if not enterprise.enemy.alive:
        print('Enemy was killed. God bless %r!'% enterprise.name)

if __name__ == '__main__':
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.