Advertisement
Ihmemies

4-14 autolla-ajelupeli

Sep 11th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.34 KB | Source Code | 0 0
  1. import math
  2.  
  3. def menu():
  4.     """
  5.    This is a text-based menu. You should ONLY TOUCH TODOs inside the menu.
  6.    TODOs in the menu call functions that you have implemented and take care
  7.    of the return values of the function calls.
  8.    """
  9.  
  10.     tank_size = read_number("How much does the vehicle's gas tank hold? ")
  11.     gas = tank_size # Tank is full when we start
  12.     gas_consumption = read_number("How many liters of gas does the car consume per hundred kilometers? ")
  13.     x = y = float(0.0)
  14.  
  15.     while True:
  16.         print(f"Coordinates x={x:.1f}, y={y:.1f}, the tank contains {gas:.1f} liters of gas.")
  17.         choice = input("1) Fill 2) Drive 3) Quit\n-> ")
  18.  
  19.         if choice == "1":
  20.             to_fill = read_number("How many liters of gas to fill up? ")
  21.             gas = fill(tank_size, to_fill, gas)
  22.  
  23.         elif choice == "2":
  24.             new_x = read_number("x: ")
  25.             new_y = read_number("y: ")
  26.             gas, x, y = drive(x, y, new_x, new_y, gas, gas_consumption)
  27.  
  28.         elif choice == "3":
  29.             break
  30.  
  31.     print("Thank you and bye!")
  32.  
  33. # Implement your own functions here. You are required to
  34. # implement at least two functions that take at least
  35. # one parameter and return at least one value.  The
  36. # functions have to be used somewhere in the program.
  37. def direction(cur_x, cur_y, dest_x, dest_y):
  38.     """
  39.    determine direction
  40.    return direction x, y or xy
  41.    """
  42.     dir = str
  43.     positive = True
  44.  
  45.     # move vertically
  46.     if cur_x == dest_x:
  47.         dir = "y"
  48.         if 0 >= (dest_y - cur_y):
  49.             positive = False
  50.  
  51.     # move horizontally
  52.     elif cur_y == dest_y:
  53.         dir = "x"
  54.         if 0 >= (dest_x - cur_x):
  55.             positive = False
  56.  
  57.     # move diagonally
  58.     else:
  59.         dir = "xy"
  60.  
  61.     #print("ajosuunta:", dir, positive)
  62.     return dir, positive
  63.  
  64. def hypotenusa(a, b):
  65.     """
  66.    calculates hypotenusa from 2 straight sides (a,b) of triangle
  67.    returns hypotenusa
  68.    """
  69.     c = math.sqrt(pow(a, 2)+pow(b, 2))
  70.     return c
  71.  
  72. def fill(tank_size: float, to_fill: float, gas: float):
  73.     """
  74.    tank_size:   the size of the tank
  75.    to_fill:     the amount of gas that is requested to be filled in
  76.    gas:         the amount of gas in the tank currently
  77.    """
  78.     if (to_fill + gas > tank_size):
  79.         return tank_size
  80.     else:
  81.         return to_fill + gas
  82.  
  83. def drive(cur_x: float, cur_y: float, dest_x: float, dest_y: float, gas: float, gas_consumption: float):
  84.     """
  85.    This function has six parameters. They are all floats.
  86.      (1) The current x coordinate
  87.      (2) The current y coordinate
  88.      (3) The destination x coordinate
  89.      (4) The destination y coordinate
  90.      (5) The amount of gas in the tank currently
  91.      (6) The consumption of gas per 100 km of the car
  92.  
  93.    The parameters have to be in this order.
  94.    The function returns three floats:
  95.      (1) The amount of gas in the tank AFTER the driving
  96.      (2) The reached (new) x coordinate
  97.      (3) The reached (new) y coordinate  
  98.    """
  99.  
  100.     final_x = final_y = float   # how far we get actually
  101.     max_dist = (gas/gas_consumption)*100
  102.     rem_gas = float
  103.     pos = bool
  104.  
  105.     # determine direction, and if movement is positive or negative in xy coords
  106.     dir, pos = direction(cur_x, cur_y, dest_x, dest_y)
  107.  
  108.     # car moves horizontally
  109.     if dir == "x":
  110.         distance = abs(dest_x - cur_x)
  111.         gas_used = (gas_consumption/100)*distance        
  112.  
  113.         #bensaa meni liikaa
  114.         if gas_used > gas:            
  115.             rem_gas = 0
  116.             if pos:
  117.                 final_x = cur_x+max_dist
  118.             else:
  119.                 final_x = cur_x-max_dist
  120.             final_y = cur_y
  121.         else:
  122.             rem_gas = gas-gas_used
  123.             final_x = dest_x
  124.             final_y = cur_y
  125.  
  126.     # car moves vertically
  127.     elif dir == "y":
  128.         distance = abs(dest_y - cur_y)
  129.         gas_used = (gas_consumption/100)*distance
  130.  
  131.         #bensaa meni liikaa
  132.         if gas_used > gas:            
  133.             rem_gas = 0
  134.             final_x = cur_x            
  135.             if pos:
  136.                 final_y = cur_y+max_dist
  137.             else:
  138.                 final_y = cur_y-max_dist
  139.         else:
  140.             rem_gas = gas-gas_used
  141.             final_x = cur_x
  142.             final_y = dest_y
  143.  
  144.     # car moves diagonally
  145.     elif dir == "xy":
  146.         distance = abs(hypotenusa((dest_x - cur_x), (dest_y - cur_y)))
  147.         gas_used = (gas_consumption/100)*distance
  148.  
  149.         #bensaa meni liikaa
  150.         if gas_used > gas:            
  151.             rem_gas = 0
  152.             percentage = max_dist/distance
  153.             final_x = dest_x*percentage
  154.             final_y = dest_y*percentage
  155.         else:
  156.             rem_gas = gas-gas_used
  157.             final_x = dest_x
  158.             final_y = dest_y
  159.     else:
  160.         print("Nyt meni jotain pieleen, väärä suunta!")
  161.  
  162.     return rem_gas, final_x, final_y  
  163.  
  164.  
  165. def read_number(prompt, error_message="Incorrect input!"):
  166.     """
  167.    DO NOT TOUCH THIS FUNCTION.
  168.    This function reads input from the user.
  169.    Also, don't worry if you don't understand it.
  170.    """
  171.     try:
  172.         return float(input(prompt))
  173.  
  174.     except ValueError:
  175.         print(error_message)
  176.         return read_number(prompt, error_message)
  177.  
  178.  
  179. def main():
  180.     """
  181.    """
  182.     menu()
  183.  
  184. if __name__ == "__main__":
  185.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement