Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture, 24.11.2022
- print("Welcome! Note: print() is also a function!")
- # FUNCTIONS FILE - functions.py
- # functions.py, like a TOOLBOX
- # functions do nothing in code
- # before they are used
- # simple text printing function
- def show_text():
- print("Welcome to application!")
- print("-----------------------")
- print("Please follow instructions.")
- print()
- # text printing function, takes 3 parameters
- def combine_text(first, last, age):
- print(f"Welcome, {first} {last}")
- print(f"You are {age} years old.")
- # function that returns a number
- def get_year():
- result = 2022
- return result
- # function that determines if number
- # is odd or even
- def get_even_number_text(number):
- if number % 2 == 0:
- return "Even."
- else:
- return "Odd."
- # helper function: how many days
- # are in certain amount of hours
- def hours_to_days(hours):
- result = hours // 24
- return result
- # helper function, reverses string
- def reverse_string(text):
- return text[::-1]
- # function that checks if given text
- # is a palindrome
- def check_palindrome(text):
- reversed_text = reverse_string(text)
- # see if texts match
- if text == reversed_text:
- return True
- else:
- return False
- # function that checks if order code
- # is in correct format: 10 characters long
- # 1st character has to be "T"
- def check_order(code):
- okay = True
- # if code is not 10 characters long => False
- if len(code) != 10:
- okay = False
- # if 1st letter not T => False
- if code[0] != "T":
- okay = False
- # helper function that prints
- # contents of a list
- def show_list(data):
- # print the contents
- for word in data:
- print(word)
- # helper function, calculates average from any list
- def get_list_average(numbers):
- total = sum(numbers)
- amount = len(numbers)
- result = total / amount
- result = round(result, 2)
- return result
- # OTHER CODE FILES
- from functions import *
- show_text()
- # NEW FILE
- from functions import *
- combine_text("Test", "Person", 31)
- # NEW FILE
- from functions import *
- year = get_year()
- print(year)
- # NEW FILE
- from functions import *
- value = input("Give a number:\n")
- value = int(value)
- # call our own function with the value
- text = get_even_number_text(value)
- print(text)
- # NEW FILE
- from functions import *
- text = input("Give a word:\n")
- result = reverse_string(text)
- print(result)
- # NEW FILE
- from functions import *
- days = hours_to_days(9746)
- print(f"{days} in total.")
- # NEW FILE
- from functions import *
- text = input("Give text:\n")
- result = check_palindrome(text)
- # check result
- if result:
- print("Palindrome!")
- else:
- print("Not a palindrome.")
- # NEW FILE
- from functions import *
- test = "F1567-9676"
- result = check_order(test)
- print(result)
- # NEW FILE
- from functions import *
- cities = ["Rovaniemi", "Madrid", "London", "Stockholm"]
- show_list(cities)
- # NEW FILE
- from functions import *
- numbers = [8, 7, 4, 5, 6, 9, 3, 4, 6]
- grades = [5, 3, 4, 2, 1, 5, 5, 4, 3, 4]
- temperatures = [-4, -5, -12, -7, -6, -11, -5]
- average_number = get_list_average(numbers)
- print(average_number)
- average_grade = get_list_average(grades)
- print(average_grade)
- average_temperature = get_list_average(temperatures)
- print(average_temperature)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement