tuomasvaltanen

Untitled

Dec 2nd, 2021 (edited)
473
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Own functions in Python
  2.  
  3. # functions.py - file
  4.  
  5. # our first function!
  6. def print_title():
  7.     print("Welcome to our app!")
  8.     print("-------------------")
  9.     print("Please follow the instructions")
  10.     print()
  11.  
  12.  
  13. # three parameters!
  14. def combine_text(first_name, surname, age):
  15.     print(f"{first_name} {surname}, {age} years old!")
  16.  
  17.  
  18. # returning data into a variable
  19. def get_year():
  20.     result = 2021
  21.     return result
  22.  
  23. # check if given number is even/odd
  24. def get_even_number_text(number):
  25.     if number % 2 == 0:
  26.         return "Even number!"
  27.     else:
  28.         return "Odd number!"
  29.  
  30. # function, hours to days
  31. def get_days(hours):
  32.     result = hours // 24
  33.     return result
  34.  
  35.  
  36. # helper function, reverse the given parameter and return it
  37. def reverse_string(text):
  38.     result = text[::-1]
  39.     return result
  40.  
  41.  
  42. # helper function, checks if given parameter
  43. # is a palindrome or not. returns either True or False
  44. def check_palindrome(text):
  45.     reversed_text = reverse_string(text)
  46.  
  47.     if text == reversed_text:
  48.         return True
  49.     else:
  50.         return False
  51.  
  52.  
  53. # example validation function
  54. # checks if given order code is okay
  55. # a valid code is 10 characters long +
  56. # first character has to be T
  57. def check_order(code):
  58.     okay = True
  59.  
  60.     if len(code) != 10:
  61.         okay = False
  62.  
  63.     if code[0] != "T":
  64.         okay = False
  65.  
  66.     return okay
  67.  
  68.  
  69. # helper function, calculates average of given number list
  70. def get_list_average(numbers):
  71.     # average = total sum / amount of numbers
  72.     total = sum(numbers)
  73.     amount = len(numbers)
  74.  
  75.     result = total / amount
  76.     return result
  77.  
  78.  
  79. # loops are also okay in a function!
  80. def show_vertical_text(text):
  81.     for c in text:
  82.         print(c.upper())
  83.  
  84.  
  85. # example recursive function
  86. def factorial(number):
  87.     if number == 1:
  88.         return number
  89.     else:
  90.         next_number = number - 1
  91.         total = number * factorial(next_number)
  92.         return total
  93.  
  94. # exercise files
  95.  
  96. from functions import *
  97.  
  98. # call our first function!
  99. print_title()
  100.  
  101. print()
  102.  
  103. # call our function that takes three parameters
  104. combine_text("John", "Doe", 31)
  105.  
  106. print()
  107.  
  108. # NEW FILE
  109.  
  110. from functions import *
  111.  
  112. # if functions returns data, remember to catch it in a variable
  113. year = get_year()
  114.  
  115. print(year)
  116.  
  117. # NEW FILE
  118.  
  119. from functions import *
  120.  
  121. # ask number from user, convert to int
  122. number = input("Give a number:\n")
  123. number = int(number)
  124.  
  125. # check if the number is even/odd with our function
  126. result = get_even_number_text(number)
  127.  
  128. # print result text
  129. print(result)
  130.  
  131. # NEW FILE
  132.  
  133. from functions import *
  134.  
  135. user_text = input("Give a text:\n")
  136.  
  137. # reverse user's text with our own function
  138. reversed_text = reverse_string(user_text)
  139.  
  140. print(reversed_text)
  141.  
  142. # NEW FILE
  143.  
  144. from functions import *
  145.  
  146. # hours from user, to int
  147. user_hours = input("How many hours?\n")
  148. user_hours = int(user_hours)
  149.  
  150. # get number of days in hours with our own function
  151. days = get_days(user_hours)
  152.  
  153. print(f"{user_hours} hours is {days} days in total!")
  154.  
  155. # NEW FILE
  156.  
  157. from functions import *
  158.  
  159. text = input("Give some text:\n")
  160.  
  161. # call our own function that checks if it's a palindrome
  162. # returns True or False
  163. result = check_palindrome(text)
  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. user_code = input("Provide a code:\n")
  175.  
  176. # check if the given code is in correct format with our own function
  177. # a good code is 10 characters long exactly and first letter is T
  178. result = check_order(user_code)
  179.  
  180. if result:
  181.     print("Code is valid!")
  182. else:
  183.     print("Incorrect code...")
  184.  
  185. # NEW FILE
  186.  
  187. from functions import *
  188.  
  189. # some number lists for testing
  190. values = [3, 5, 7, 8, 4, 5, 3, 2]
  191. temperatures = [-5, -11, -15, -12, -13, -25]
  192. grades = [3, 4, 2, 3, 3, 1, 5, 5]
  193.  
  194. # let's use our average function three times!
  195. # quite handy? :)
  196.  
  197. # get average of numbers
  198. average = get_list_average(values)
  199. print(round(average, 2))
  200.  
  201. # get average of temperatures
  202. average = get_list_average(temperatures)
  203. print(round(average, 1))
  204.  
  205. # get average of grades
  206. average = get_list_average(grades)
  207. print(average)
  208.  
  209. # NEW FILE
  210.  
  211. from functions import *
  212.  
  213. show_vertical_text("hotel")
  214.  
  215. # NEW FILE
  216.  
  217. from functions import factorial
  218.  
  219. # call the recursive function
  220. # (a function that calls itself repeatedly)
  221. result = factorial(6)
  222. print(result)
RAW Paste Data