#Lander Part 2: Isabella Krynyckyi, Amy Rehnert, & Cody DeSantis """ these are the global scopes and attributes of the class Lander Attributes: altitude = 400.0 in meters; m, a float speed = 40.0 in meters per second; m/s, a float fuel = 25 fuel in units, a float strength = 4 in units, a float gravity = 1.622 in meters per second squared; m/s**2, a float """ altitude = 400.0 speed = 40.0 fuel = 25 strength = 4 GRAVITY = 1.622 def greeting(): """ Greets the player and provides basic instructions for operation No Parameters No Returns """ print("Welcome to a Lander Simulation. Your goal is to land the lander on the") print("surface of the moon while the moon's gravity is accelerating you ship's decent") print("RAPIDLY!") print("The only thing that you have to do is slow the ship down to a safe enough speed") print("for a soft landing is one button that controls the ship's reverse thrusters") print("The more strength that is applied to the button the greater the thruster will be") print("BUT BE CAREFULY! Your ship has a limited amount of fuel. If you run out you willcrash!") print("First you must set your own altitude, velocity, and fuel:") def get_manual_or_automatic(): """ This function takes the user input and determines whether to run an automatic or manual simulation. No Parameters Returns: m or M, a string a or A, a string """ print ("Would you like to run a manual (m) or automatic (a) simulation?") while True: x = input() if x== "a" or x == "A": return "a" if x == "m" or x == "M": return "m" def get_lander(): """ This function asks the user to enter values for the lander's initial altitude, speed, and fuel. Parameters: None Returns: l, the instances of Lander for all the values whihc were input """ l = Lander() print("Enter a float for the landers intial altitude:") l.altitude = get_float() print("Enter a float for the landers intial speed:") l.speed = get_float() print("Enter an integer for the landers intial fuel:") l.fuel = get_integer(100) l.thrust =0 l.altitudeHistory = [l.altitude] l.speedHistory = [l.speed] l.fuelHistory = [l.fuel] l.thrustHistory = [l.thrust] return l class Lander(): """ This class displays the status of the Lander. Attributes: altitude, float speed, float fuel, float strength, float altitudeHistory, a list speedHistory, a list fuelHistory, a list thrustHistory, a list """ altitude = 400.0 speed = 40.0 fuel = 25 strength = 4 altitudeHistory= [] speedHistory= [] fuelHistory= [] thrustHistory= [] landed = False l = Lander() def get_response(string): """ This function determines if an input is a string. Parameters: string, a string Returns: a, a string """ while True: print("Please enter one of the following characters: " , string) a = str.lower(input()) if a in string: return a else: continue def human_controller(l): """ This aquires the thrust of the lander from user and returns the value as an integer. Parameter: l, instance of lander Return: amount, integer """ print ("Please enter a number between 0 and 4:") while True: amount = int(input()) if amount >l.fuel or amount > l.strength or amount < 0 : print ("Please enter a number between 0 and 4:") pass else: return amount def get_integer(maximum): """ This function takes an input and checks to see if it is between 0 and the set maximum. If the input is outside the range it will ask the user to keep entering numbers until an acceptable input is entered Parameters: maximum, an input of an int Returns: an integer """ while True: a = input() while not a.isdigit(): print("Please enter a number.") a =input() a=int(a) if a in range (0, maximum+1): return a elif maximum <0: return a def is_float(string): """ This function takes a string as an argument and returns a boolean. Parameters: s, a string Returns: True, a Boolean False, a Boolean """ try: float(string) return True except: return False def get_float(): """ This function gets a floating-point value from the user and returns the float. No Parameters Returns: """ while True: z = input() if is_float(z): return float(z) else: continue print("Please enter a proper float.") def show_history(lander): """ Function which takes an instance of Lander and prints the history of the lander. Parameter: lander, an instance of Lander """ y = 0 while y<len(lander.altitudeHistory): print ("Alt =", lander.altitudeHistory[y], "Vel =" , lander.speedHistory[y], "Fuel =", lander.fuelHistory[y], "Str =", 4) print ("Thrust =", lander.thrustHistory[y]) y += 1 def thrust(l,amount): """ Function that determines if amount is greater than the remaining fuel or the strength of the lander, reduces it to the smaller of those two values, and reduces the fuel in the lander by amount """ if l.strength> l.fuel and (amount>l.fuel or amount>l.strength): amount = l.fuel elif l.fuel > l.strength and (amount>l.fuel or amount>l.strength): amount = l.strength l.fuel -= amount l.speed-=(4*amount) l.thrustHistory.append(amount) l.fuelHistory.append(l.fuel) def update_onesecond(l): """ This updates the speed and altitude of the lander. Parameters: l, an instance of lander Returns: True, Boolean False, Boolean """ l.speed += GRAVITY l.speedHistory.append(l.speed) l.altitude = l.altitude-l.speed if l.altitude <= 0.0: l.altitude = 0.0 l.altitudeHistory.append(l.altitude) if l.altitude>0.0: l.altitudeHistory.append(l.altitude) return True else: return False def simulate_landing(l,b): """ Function used to indicate whether the human_controller or smart_controller is in charge Parameters: l, instance of Lander b, Boolean Return: True, Boolean False, Boolean """ while b: if l.altitude> 0.0: show_status (l) thrust (l, human_controller (l)) update_onesecond(l) elif l.altitude == 0 and l.speed <= l.strength: l.landed = True print("Success! Congratulations, you have landed succesfully!") return True else: print("You have crashed! Try again!") return False while not b: if l.altitude > 0.0: show_status (l) thrust (l, smart_controller (l)) update_onesecond (l) l.landed = True elif l.altitude == 0 and l.speed<= l.strength: return True else: return False def show_status(l): print ("Altitude =", round(l.altitude, 2), "Velocity =" , round(l.speed, 2), "Fuel =", l.fuel, "Strength =", l.strength) GRAVITY = 1.622 #in m/s^2. the gravity on the moon def main (): greeting() #listoflanders = [update_onesecond.append()] listoflanders=[] #get_integer(maximum) #pilot= get_manual_or_automatic() """if pilot == "m": human_controller(l) update_onesecond(l) get_float() check = is_float(string) if check == True: print("get_float and is_float works.") else: print("run get float again") elif pilot == "a": print("This function (smart_controller) is not needed for part 2 of the project") """ answer = "a" #answer = get_response("qra") while True: if answer == "a": listoflanders.append(get_lander()) while listoflanders[-1].altitude>0: simulate_landing(listoflanders[-1],True) print("Please enter a if you want to try again, r if you want to review your play history, and q to quit.") answer = get_response("qra") elif answer == "r": n = len(listoflanders) print("You have run", n, "simulations.") oneofthelanders = -1 while (oneofthelanders >= n or oneofthelanders< 0): print("Please enter a non-negative value representing the simulation you would like to review. It has to be zero or more and less than",n) oneofthelanders = int(input()) show_history(listoflanders[oneofthelanders]) print("Please enter a if you want to try again, r if you want to review your play history, and q to quit.") answer= get_response("qra") elif answer== "q": return listoflanders break
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question