Advertisement
Ihmemies

Untitled

Sep 10th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | Source Code | 0 0
  1. """
  2. 3-3-1 Ohjelma, joka vertailee inflaatiolukuja keskenään ja tulostaa suurimman nousun
  3.  
  4. Aloitettu 16:07
  5. Valmis 16:44
  6. """
  7.  
  8. def main():
  9.     month = 1
  10.     prev_month = False      # for first month handling
  11.     monthly_changes = []    # collect list of all monthly charges, sort them and print out largest change
  12.     largest_change = float
  13.  
  14.     # ask input from user until input is empty (user presses enter)
  15.     while True:
  16.         user_input = input(f"Enter inflation rate for month {month}: ")
  17.  
  18.         # program logic
  19.         if user_input != "":
  20.             # handle first month input
  21.             if not prev_month:
  22.                 prev_month = float(user_input)            
  23.             else:
  24.                 monthly_changes.append(float(user_input)-prev_month)        
  25.  
  26.             # test print      
  27.             # print(f"prev: {prev_month}, input: {user_input}, change: {largest_change:.1f}")
  28.             prev_month = float(user_input)
  29.  
  30.         # errors, exiting program
  31.         if month <= 2 and user_input == "":
  32.             print("Error: at least 2 monthly inflation rates must be entered.")
  33.             break
  34.        
  35.         # sorts from small to largest list item
  36.         # -1 picks last item from list, which is largest
  37.         elif month >= 2 and user_input == "":
  38.             monthly_changes.sort()  
  39.             largest_change = f"{monthly_changes[-1]:.1f}"
  40.             print(f"Maximum inflation rate change was {largest_change} points.")
  41.             break
  42.  
  43.         month += 1
  44.  
  45. if __name__ == "__main__":
  46.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement