[PYTHON] Lunar Lander

#Lunar Lander Simulation
#Kevin Wong - kmw396
#CS164-069 / Lecture B

#this function will ask the user the amount fuel they want to use
def ask_fuel(current_fuel):
    valid = False 
    while not valid:
        try:
            f = input("Enter units of fuel to use: ")
            f = int(f)
            # if the user enters anything thats less than 0, it won't accept it.
            if f < 0:
                print("Cannot use negative fuel.")
                valid = False
            # if the user enters an amount that doesn't exist, it won't accept it.
            elif f > current_fuel:
                print("Not enough fuel. Max Fuel:", current_fuel)
            # if the user enters the correct amount, it will accept it
            elif f >=0 and f <= current_fuel:
                return f
        # if non-int value, it will ask again.
        except ValueError as e:
            print("Please Enter an Integer Value.")
            
def play_level(name,gravity,fuel):
    altitude = 50 #altitude (m)
    velocity = 0 #inital velocity (m/s)
    s = 0 #number of seconds passed
    T = 0.1 #thruster accerlation (m/s^2)

    #prints the stats
    print("Entering Level",level)
    print("Landing on the",name)
    print("Gravity is %.2f m/s^2" %gravity)
    print("Inital Altitude:",altitude,"meters")
    print("Initial Velocity: %.2f m/s" %velocity)
    print("Burning a unit of fuel causes 0.10 m/s slowdown.")
    print("Inital Fuel Level:",fuel)
    print("\nGO")

    #loop for the game
    while altitude != 0:
        #asks user for the amt. of fuel they want to use
        f = ask_fuel(fuel) 
        #calculates the velocity
        velocity = velocity + gravity + T * f
        #calculates the altitude
        altitude += velocity
        #when user enters amt of fuel they want to use, it will subtract from the total amount available and give back whats left 
        fuel -= f
        #adds 1 to the time counter through every succession
        s += 1
        #if the altitude is lower than or equal to 0, altitude becomes 0
        if altitude <= 0:
            altitude = 0
        print("After",s,"seconds, the Altitude is %.2f meters, and Velocity is %.2f m/s." %(altitude,velocity))
        print("Remaining Fuel:",fuel,"units.")
    
    #if the altitude is 0, the function will run
    if altitude == 0:
        #a successful landing is where the velocity is between -2 an 2
        if velocity <= 2 and velocity >= -2:
            print("Landed Successfully.")
            final = 0
            return final
        else:
            print("Crashed!")
            final = 1
            return final

level = 1 #stores level information and starts level at 1

print("Welcome to Lunar Lander Game.")
#it will run the loop until it reaches 11
while level <= 11:
    play = input("Do you want to play %d? (yes/no)\n" %level)
    if play.lower() == 'yes': #if the user says yes, it will start the game.
    #provides necesscary information for each level so the function play_level runs.
        if level == 1:
            moon = play_level('Moon',-1.622,150)
            if moon == 0:
                level += 1
            elif moon == 1:
                level = 1
                
        elif level == 2:
            earth = play_level('Earth',-9.81,5000)
            if earth == 0:
                level += 1
            elif earth == 1:
                level = 2
                
        elif level == 3:
            pluto = play_level('Pluto',-0.42,1000)
            if pluto == 0:
                level += 1
            elif pluto == 1:
                level = 3
                
        elif level == 4:
            neptune = play_level('Neptune',-14.07,1000)
            if neptune == 0:
                level += 1
            elif neptune == 1:
                level = 4
                
        elif level == 5:
            uranus = play_level('Uranus',-10.67,1000)
            if uranus == 0:
                level += 1
            elif uranus == 1:
                level = 5
                
        elif level == 6:
            saturn = play_level('Saturn',-11.08,1000)
            if saturn == 0:
                level += 1
            elif saturn == 1:
                level = 6
                
        elif level == 7:
            jupiter = play_level('Jupiter',-25.95,1000)
            if jupiter == 0:
                level += 1
            elif jupiter == 1:
                level = 7
                
        elif level == 8:
            mars = play_level('Mars',-3.77,1000)
            if mars == 0:
                level += 1
            elif mars == 1:
                level = 8
                
        elif level == 9:
            venus = play_level('Venus',-8.87,1000)
            if venus == 0:
                level += 1
            elif venus == 1:
                level = 9

        elif level == 10:
            mercury = play_level('Mercury',-3.59,1000)
            if mercury == 0:
                level += 1
            elif mercury == 1:
                level = 10

        elif level == 11:
            sun = play_level('Sun',-274.13,50000)
            if sun == 0:
                level += 1
            elif sun == 1:
                level = 11
                
    elif play.lower() == 'no': #if the user says no, it will quit and display their stats.
        level -= 1
        print("You made it past",level,"levels.")
        print("Thanks for playing!")
        exit()