Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Introduction to Programming, functions, 25.11.2024
- print("Welcome!")
- # functions.py, this file contains
- # the definitions of all our own functions
- # kinda like a toolkit!
- # define a function show_text()
- # => run this code, if somebody
- # happened to call this function
- # somewhere else
- def show_text():
- print("Welcome to our app!")
- print("-------------------")
- print("Please follow instructions.")
- print()
- # another function: combine_text
- # three parameters: first name, surname and age
- def combine_text(first, last, age):
- print(f"Welcome: {first} {last}")
- print(f"You are {age} years old.")
- # define a function that returns data
- # => remember to save the result into a variable
- def get_year():
- result = 2024
- return result
- # define a function that determines if number
- # is odd or even
- def get_even_number_text(number):
- if number % 2 == 0:
- return "Even"
- else:
- return "Odd"
- # define a function that counts
- # days within hours and returns the result
- def hours_to_days(hours):
- result = hours // 24
- return result
- # define a function that reverses a string
- # this is a handy way to hide the weird
- # Python way which reverses the string
- def reverse_string(text):
- return text[::-1]
- # define a function that checks if the given
- # text is a palindrome or not
- # (text is identical also when reversed)
- def check_palindrome(text):
- reversed_text = reverse_string(text)
- # check if text is identical also while reversed,
- # return a boolean
- if text == reversed_text:
- return True
- else:
- return False
- # define a function that checks if the given
- # order code follows the needed pattern
- # exactly 10 characters long, first letter is T
- def check_order(code):
- result = True
- # condition 1: length has to be exactly 10
- if len(code) != 10:
- result = False
- # condition 2: first letter has to be T
- if code[0] != "T":
- result = False
- return result
- # define a helper function that prints
- # the content of a list for us
- def show_list(data):
- # loop through the list and print content
- for word in data:
- print(f"{word}")
- # define a helper function
- # that calculates the average of any
- # given numeric list
- def get_list_average(numbers):
- total = sum(numbers)
- amount = len(numbers)
- # calculate average and round
- result = total / amount
- result = round(result, 1)
- return result
- ###########################################
- # ACTUAL CODE FILES AFTER THIS
- from functions import *
- # call our own function from functions.py
- show_text()
- print()
- # call our own function with parameters
- combine_text("John", "Doe", 31)
- # NEW FILE
- from functions import *
- # call a function that returns a value
- year = get_year()
- print(year)
- # NEW FILE
- from functions import *
- # ask the number from user
- value = input("Give a number:\n")
- value = int(value)
- # use our own function to decide if number is odd/even
- result = get_even_number_text(value)
- print(result)
- # NEW FILE
- from functions import *
- # reverse the text from user
- text = input("Give a text:\n")
- result = reverse_string(text)
- print(result)
- # NEW FILE
- from functions import *
- # ask the hours from user and convert
- # them into number of days
- hours = input("How many hours?\n")
- hours = int(hours)
- days = hours_to_days(hours)
- print(f"{days} days in total.")
- # NEW FILE
- from functions import *
- text = input("Give a word:\n")
- # use our own function to check
- # if this text is palindrome or not
- result = check_palindrome(text)
- # check the boolean that
- # our functions returns
- if result:
- print("Palindrome!")
- else:
- print("Not a palindrome...")
- # NEW FILE
- from functions import *
- # ask user for an order code
- text = input("Give an order code:\n")
- # use our function to check if the code
- # has the required features (validation function)
- result = check_order(text)
- # interpret the result
- if result:
- print("Code OK!")
- else:
- print("Code not OK...")
- # NEW FILE
- from functions import *
- # some list data
- cities = ["Rovaniemi", "Helsinki", "Oulu", "Stockholm"]
- # call our own helper function that prints the contents of a list
- show_list(cities)
- # NEW FILE
- from functions import *
- # three different lists for calculating the averages
- values = [5, 6, 8, 3, 4, 7, 8, 6]
- grades = [0, 3, 2, 5, 4, 3, 4, 5, 3, 4]
- temperatures = [-5.6, -3.2, -11.8, -7.8]
- # use our own helper function for all three lists separately
- average = get_list_average(values)
- print(f"Average: {average}")
- average = get_list_average(grades)
- print(f"Average: {average}")
- average = get_list_average(temperatures)
- print(f"Average: {average}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement