Advertisement
tuomasvaltanen

Untitled

Nov 23rd, 2022 (edited)
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. # lecture, 24.11.2022
  2. print("Welcome! Note: print() is also a function!")
  3.  
  4. # FUNCTIONS FILE - functions.py
  5.  
  6. # functions.py, like a TOOLBOX
  7. # functions do nothing in code
  8. # before they are used
  9.  
  10. # simple text printing function
  11. def show_text():
  12.     print("Welcome to application!")
  13.     print("-----------------------")
  14.     print("Please follow instructions.")
  15.     print()
  16.  
  17.  
  18. # text printing function, takes 3 parameters
  19. def combine_text(first, last, age):
  20.     print(f"Welcome, {first} {last}")
  21.     print(f"You are {age} years old.")
  22.  
  23.  
  24. # function that returns a number
  25. def get_year():
  26.     result = 2022
  27.     return result
  28.  
  29.  
  30. # function that determines if number
  31. # is odd or even
  32. def get_even_number_text(number):
  33.     if number % 2 == 0:
  34.         return "Even."
  35.     else:
  36.         return "Odd."
  37.  
  38. # helper function: how many days
  39. # are in certain amount of hours
  40. def hours_to_days(hours):
  41.     result = hours // 24
  42.     return result
  43.  
  44.  
  45. # helper function, reverses string
  46. def reverse_string(text):
  47.     return text[::-1]
  48.  
  49.  
  50. # function that checks if given text
  51. # is a palindrome
  52. def check_palindrome(text):
  53.     reversed_text = reverse_string(text)
  54.  
  55.     # see if texts match
  56.     if text == reversed_text:
  57.         return True
  58.     else:
  59.         return False
  60.  
  61. # function that checks if order code
  62. # is in correct format: 10 characters long
  63. # 1st character has to be "T"
  64. def check_order(code):
  65.     okay = True
  66.  
  67.     # if code is not 10 characters long => False
  68.     if len(code) != 10:
  69.         okay = False
  70.  
  71.     # if 1st letter not T => False
  72.     if code[0] != "T":
  73.         okay = False
  74.  
  75. # helper function that prints
  76. # contents of a list
  77. def show_list(data):
  78.  
  79.     # print the contents
  80.     for word in data:
  81.         print(word)
  82.  
  83.  
  84. # helper function, calculates average from any list
  85. def get_list_average(numbers):
  86.     total = sum(numbers)
  87.     amount = len(numbers)
  88.  
  89.     result = total / amount
  90.     result = round(result, 2)
  91.  
  92.     return result
  93.  
  94. # OTHER CODE FILES
  95.  
  96. from functions import *
  97.  
  98. show_text()
  99.  
  100. # NEW FILE
  101.  
  102. from functions import *
  103.  
  104. combine_text("Test", "Person", 31)
  105.  
  106. # NEW FILE
  107.  
  108. from functions import *
  109.  
  110. year = get_year()
  111. print(year)
  112.  
  113. # NEW FILE
  114.  
  115. from functions import *
  116.  
  117. value = input("Give a number:\n")
  118. value = int(value)
  119.  
  120. # call our own function with the value
  121. text = get_even_number_text(value)
  122. print(text)
  123.  
  124. # NEW FILE
  125.  
  126. from functions import *
  127.  
  128. text = input("Give a word:\n")
  129.  
  130. result = reverse_string(text)
  131. print(result)
  132.  
  133. # NEW FILE
  134.  
  135. from functions import *
  136.  
  137. days = hours_to_days(9746)
  138. print(f"{days} in total.")
  139.  
  140. # NEW FILE
  141.  
  142. from functions import *
  143.  
  144. text = input("Give text:\n")
  145.  
  146. result = check_palindrome(text)
  147.  
  148. # check result
  149. if result:
  150.     print("Palindrome!")
  151. else:
  152.     print("Not a palindrome.")
  153.  
  154. # NEW FILE
  155.  
  156. from functions import *
  157.  
  158. test = "F1567-9676"
  159.  
  160. result = check_order(test)
  161. print(result)
  162.  
  163. # NEW FILE
  164.  
  165. from functions import *
  166.  
  167. cities = ["Rovaniemi", "Madrid", "London", "Stockholm"]
  168.  
  169. show_list(cities)
  170.  
  171. # NEW FILE
  172.  
  173. from functions import *
  174.  
  175. numbers = [8, 7, 4, 5, 6, 9, 3, 4, 6]
  176. grades = [5, 3, 4, 2, 1, 5, 5, 4, 3, 4]
  177. temperatures = [-4, -5, -12, -7, -6, -11, -5]
  178.  
  179. average_number = get_list_average(numbers)
  180. print(average_number)
  181.  
  182. average_grade = get_list_average(grades)
  183. print(average_grade)
  184.  
  185. average_temperature = get_list_average(temperatures)
  186. print(average_temperature)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement