tuomasvaltanen

Untitled

Nov 22nd, 2023 (edited)
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. # functions.py, this contains
  2. # the definitions of all our own functions
  3. # Consider this as a toolkit!
  4.  
  5. # define a function show_text()
  6. # => only shows some text to user
  7. def show_text():
  8.     print("Welcome to our app!")
  9.     print("-------------------")
  10.     print("Please follow instructions.")
  11.     print()
  12.  
  13.  
  14. # another function: combine_text
  15. # three params: first name, surname, age
  16. def combine_text(first, last, age):
  17.     print(f"Welcome: {first} {last}!")
  18.     print(f"You are {age} years old.")
  19.  
  20.  
  21. # define a function that returns data
  22. # => remember to save the result into
  23. # a variable
  24. def get_year():
  25.     result = 2023
  26.     return result
  27.  
  28. # define a function that
  29. # determines if number is odd or even
  30. def get_even_number_text(number):
  31.     if number % 2 == 0:
  32.         return "Even"
  33.     else:
  34.         return "Odd"
  35.  
  36.  
  37. # define a function that counts
  38. # days within hours and returns them
  39. def hours_to_days(hours):
  40.     result = hours // 24
  41.     return result
  42.  
  43.  
  44. # define a function that reverses a string
  45. # this is a handy way to hide the weird
  46. # Python way which reverses a string
  47. def reverse_string(text):
  48.     return text[::-1]
  49.  
  50.  
  51. # define a function that checks
  52. # if given text is palindrome or not
  53. # (text is same also reversed)
  54. def check_palindrome(text):
  55.     reversed_text = reverse_string(text)
  56.     # check if text is same both ways, return a boolean
  57.     if text == reversed_text:
  58.         return True
  59.     else:
  60.         return False
  61.  
  62.  
  63. # define a function that checks is given
  64. # order code follows needed pattern
  65. # exactly 10 characters long, first letter is T
  66. def check_order(code):
  67.     result = True
  68.  
  69.     # condition 1: length has to be exactly 10
  70.     if len(code) != 10:
  71.         result = False
  72.  
  73.     # condition 2: first letter has to be T
  74.     if code[0] != "T":
  75.         result = False
  76.  
  77.     # return the result, whatever it is at this point
  78.     return result
  79.  
  80.  
  81. # define a function that
  82. # calculates the average of
  83. # any numeric list
  84. def get_list_average(numbers):
  85.     total = sum(numbers)
  86.     amount = len(numbers)
  87.     result = total / amount
  88.     result = round(result, 2)
  89.     return result
  90.  
  91. # ACTUAL CODE FILES AFTER THIS
  92.  
  93. from functions import *
  94.  
  95. # call our function
  96. show_text()
  97.  
  98. # print empty line
  99. print()
  100.  
  101. # call our own function with parameters
  102. combine_text("John", "Doe", 31)
  103.  
  104. print("App stopped!")
  105.  
  106. # NEW FILE
  107.  
  108. from functions import *
  109.  
  110. # call our own function
  111. # this function returns data
  112. # => we need to save it to a variable
  113. # compare to round() -function
  114. year = get_year()
  115.  
  116. print(year)
  117. print()
  118.  
  119. # NEW FILE
  120.  
  121. from functions import *
  122.  
  123. # ask integer from user
  124. user_number = input("Give a number:\n")
  125. user_number = int(user_number)
  126.  
  127. # test our function with a value
  128. result = get_even_number_text(user_number)
  129.  
  130. # print the returned value
  131. print(result)
  132.  
  133. # NEW FILE
  134.  
  135. from functions import *
  136.  
  137. # let's call our helper function
  138. # that reverses the text (and hides the complex Python process)
  139. text = reverse_string("what about this sentence")
  140.  
  141. print(text)
  142.  
  143. # NEW FILE
  144.  
  145. from functions import *
  146.  
  147. hours = input("Give amount of hours:\n")
  148. hours = int(hours)
  149.  
  150. # use our own function to count amount of days in these hours
  151. days = hours_to_days(hours)
  152.  
  153. print(f"{days} days in total.")
  154.  
  155. # NEW FILE
  156.  
  157. from functions import *
  158.  
  159. word = input("Give a sentence:\n")
  160.  
  161. # use our own function to see if this is palindrome or not
  162. # result will be a boolean : False/True
  163. result = check_palindrome(word)
  164.  
  165. if result:
  166.     print("Palindrome!")
  167. else:
  168.     print("Not a palindrome.")
  169.  
  170. # NEW FILE
  171.  
  172. from functions import *
  173.  
  174. order = input("Give a order code:\n")
  175.  
  176. # use our own function to check if code
  177. # is of expected format: False/True
  178. result = check_order(order)
  179.  
  180. if result:
  181.     print("Order code OK!")
  182. else:
  183.     print("Invalid order code.")
  184.  
  185. # NEW FILE
  186.  
  187. from functions import *
  188.  
  189. # define our number lists
  190. numbers = [5, 7, 9, 8, 5, 6, 7, 4, 9, 10, 9]
  191. grades = [1, 5, 4, 5, 3, 2, 3, 4, 1, 4, 5]
  192. temperatures = [-8.5, -9.7, -6.5, -24.1, -20.7, -11.6]
  193.  
  194. # get average for numbers
  195. avg_numbers = get_list_average(numbers)
  196. print(avg_numbers)
  197.  
  198. # get average for grades
  199. avg_grades = get_list_average(grades)
  200. print(avg_grades)
  201.  
  202. # get average for temperatures
  203. avg_temperatures = get_list_average(temperatures)
  204. print(avg_temperatures)
Advertisement
Add Comment
Please, Sign In to add comment