Advertisement
tuomasvaltanen

Untitled

Nov 25th, 2024 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. # Introduction to Programming, functions, 25.11.2024
  2. print("Welcome!")
  3.  
  4. # functions.py, this file contains
  5. # the definitions of all our own functions
  6. # kinda like a toolkit!
  7.  
  8. # define a function show_text()
  9. # => run this code, if somebody
  10. # happened to call this function
  11. # somewhere else
  12. def show_text():
  13.     print("Welcome to our app!")
  14.     print("-------------------")
  15.     print("Please follow instructions.")
  16.     print()
  17.  
  18.  
  19. # another function: combine_text
  20. # three parameters: first name, surname and age
  21. def combine_text(first, last, age):
  22.     print(f"Welcome: {first} {last}")
  23.     print(f"You are {age} years old.")
  24.  
  25.  
  26. # define a function that returns data
  27. # => remember to save the result into a variable
  28. def get_year():
  29.     result = 2024
  30.     return result
  31.  
  32.  
  33. # define a function that determines if number
  34. # is odd or even
  35. def get_even_number_text(number):
  36.     if number % 2 == 0:
  37.         return "Even"
  38.     else:
  39.         return "Odd"
  40.  
  41.  
  42. # define a function that counts
  43. # days within hours and returns the result
  44. def hours_to_days(hours):
  45.     result = hours // 24
  46.     return result
  47.  
  48.  
  49. # define a function that reverses a string
  50. # this is a handy way to hide the weird
  51. # Python way which reverses the string
  52. def reverse_string(text):
  53.     return text[::-1]
  54.  
  55.  
  56. # define a function that checks if the given
  57. # text is a palindrome or not
  58. # (text is identical also when reversed)
  59. def check_palindrome(text):
  60.     reversed_text = reverse_string(text)
  61.  
  62.     # check if text is identical also while reversed,
  63.     # return a boolean
  64.     if text == reversed_text:
  65.         return True
  66.     else:
  67.         return False
  68.  
  69.  
  70. # define a function that checks if the given
  71. # order code follows the needed pattern
  72. # exactly 10 characters long, first letter is T
  73. def check_order(code):
  74.     result = True
  75.  
  76.     # condition 1: length has to be exactly 10
  77.     if len(code) != 10:
  78.         result = False
  79.  
  80.     # condition 2: first letter has to be T
  81.     if code[0] != "T":
  82.         result = False
  83.  
  84.     return result
  85.  
  86.  
  87. # define a helper function that prints
  88. # the content of a list for us
  89. def show_list(data):
  90.  
  91.     # loop through the list and print content
  92.     for word in data:
  93.         print(f"{word}")
  94.  
  95.  
  96. # define a helper function
  97. # that calculates the average of any
  98. # given numeric list
  99. def get_list_average(numbers):
  100.     total = sum(numbers)
  101.     amount = len(numbers)
  102.  
  103.     # calculate average and round
  104.     result = total / amount
  105.     result = round(result, 1)
  106.     return result
  107.  
  108.  
  109. ###########################################
  110. # ACTUAL CODE FILES AFTER THIS
  111.  
  112. from functions import *
  113.  
  114. # call our own function from functions.py
  115. show_text()
  116.  
  117. print()
  118.  
  119. # call our own function with parameters
  120. combine_text("John", "Doe", 31)
  121.  
  122. # NEW FILE
  123.  
  124. from functions import *
  125.  
  126. # call a function that returns a value
  127. year = get_year()
  128. print(year)
  129.  
  130. # NEW FILE
  131.  
  132. from functions import *
  133.  
  134. # ask the number from user
  135. value = input("Give a number:\n")
  136. value = int(value)
  137.  
  138. # use our own function to decide if number is odd/even
  139. result = get_even_number_text(value)
  140. print(result)
  141.  
  142. # NEW FILE
  143.  
  144. from functions import *
  145.  
  146. # reverse the text from user
  147. text = input("Give a text:\n")
  148. result = reverse_string(text)
  149. print(result)
  150.  
  151. # NEW FILE
  152.  
  153. from functions import *
  154.  
  155. # ask the hours from user and convert
  156. # them into number of days
  157. hours = input("How many hours?\n")
  158. hours = int(hours)
  159.  
  160. days = hours_to_days(hours)
  161. print(f"{days} days in total.")
  162.  
  163. # NEW FILE
  164.  
  165. from functions import *
  166.  
  167. text = input("Give a word:\n")
  168.  
  169. # use our own function to check
  170. # if this text is palindrome or not
  171. result = check_palindrome(text)
  172.  
  173. # check the boolean that
  174. # our functions returns
  175. if result:
  176.     print("Palindrome!")
  177. else:
  178.     print("Not a palindrome...")
  179.  
  180. # NEW FILE
  181.  
  182. from functions import *
  183.  
  184. # ask user for an order code
  185. text = input("Give an order code:\n")
  186.  
  187. # use our function to check if the code
  188. # has the required features (validation function)
  189. result = check_order(text)
  190.  
  191. # interpret the result
  192. if result:
  193.     print("Code OK!")
  194. else:
  195.     print("Code not OK...")
  196.  
  197. # NEW FILE
  198.  
  199. from functions import *
  200.  
  201. # some list data
  202. cities = ["Rovaniemi", "Helsinki", "Oulu", "Stockholm"]
  203.  
  204. # call our own helper function that prints the contents of a list
  205. show_list(cities)
  206.  
  207. # NEW FILE
  208.  
  209. from functions import *
  210.  
  211. # three different lists for calculating the averages
  212. values = [5, 6, 8, 3, 4, 7, 8, 6]
  213. grades = [0, 3, 2, 5, 4, 3, 4, 5, 3, 4]
  214. temperatures = [-5.6, -3.2, -11.8, -7.8]
  215.  
  216. # use our own helper function for all three lists separately
  217. average = get_list_average(values)
  218. print(f"Average: {average}")
  219.  
  220. average = get_list_average(grades)
  221. print(f"Average: {average}")
  222.  
  223. average = get_list_average(temperatures)
  224. print(f"Average: {average}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement