Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Own functions in Python
- # functions.py - file
- # our first function!
- def print_title():
- print("Welcome to our app!")
- print("-------------------")
- print("Please follow the instructions")
- print()
- # three parameters!
- def combine_text(first_name, surname, age):
- print(f"{first_name} {surname}, {age} years old!")
- # returning data into a variable
- def get_year():
- result = 2021
- return result
- # check if given number is even/odd
- def get_even_number_text(number):
- if number % 2 == 0:
- return "Even number!"
- else:
- return "Odd number!"
- # function, hours to days
- def get_days(hours):
- result = hours // 24
- return result
- # helper function, reverse the given parameter and return it
- def reverse_string(text):
- result = text[::-1]
- return result
- # helper function, checks if given parameter
- # is a palindrome or not. returns either True or False
- def check_palindrome(text):
- reversed_text = reverse_string(text)
- if text == reversed_text:
- return True
- else:
- return False
- # example validation function
- # checks if given order code is okay
- # a valid code is 10 characters long +
- # first character has to be T
- def check_order(code):
- okay = True
- if len(code) != 10:
- okay = False
- if code[0] != "T":
- okay = False
- return okay
- # helper function, calculates average of given number list
- def get_list_average(numbers):
- # average = total sum / amount of numbers
- total = sum(numbers)
- amount = len(numbers)
- result = total / amount
- return result
- # loops are also okay in a function!
- def show_vertical_text(text):
- for c in text:
- print(c.upper())
- # example recursive function
- def factorial(number):
- if number == 1:
- return number
- else:
- next_number = number - 1
- total = number * factorial(next_number)
- return total
- # exercise files
- from functions import *
- # call our first function!
- print_title()
- print()
- # call our function that takes three parameters
- combine_text("John", "Doe", 31)
- print()
- # NEW FILE
- from functions import *
- # if functions returns data, remember to catch it in a variable
- year = get_year()
- print(year)
- # NEW FILE
- from functions import *
- # ask number from user, convert to int
- number = input("Give a number:\n")
- number = int(number)
- # check if the number is even/odd with our function
- result = get_even_number_text(number)
- # print result text
- print(result)
- # NEW FILE
- from functions import *
- user_text = input("Give a text:\n")
- # reverse user's text with our own function
- reversed_text = reverse_string(user_text)
- print(reversed_text)
- # NEW FILE
- from functions import *
- # hours from user, to int
- user_hours = input("How many hours?\n")
- user_hours = int(user_hours)
- # get number of days in hours with our own function
- days = get_days(user_hours)
- print(f"{user_hours} hours is {days} days in total!")
- # NEW FILE
- from functions import *
- text = input("Give some text:\n")
- # call our own function that checks if it's a palindrome
- # returns True or False
- result = check_palindrome(text)
- if result:
- print("PALINDROME!")
- else:
- print("Not a palindrome...")
- # NEW FILE
- from functions import *
- user_code = input("Provide a code:\n")
- # check if the given code is in correct format with our own function
- # a good code is 10 characters long exactly and first letter is T
- result = check_order(user_code)
- if result:
- print("Code is valid!")
- else:
- print("Incorrect code...")
- # NEW FILE
- from functions import *
- # some number lists for testing
- values = [3, 5, 7, 8, 4, 5, 3, 2]
- temperatures = [-5, -11, -15, -12, -13, -25]
- grades = [3, 4, 2, 3, 3, 1, 5, 5]
- # let's use our average function three times!
- # quite handy? :)
- # get average of numbers
- average = get_list_average(values)
- print(round(average, 2))
- # get average of temperatures
- average = get_list_average(temperatures)
- print(round(average, 1))
- # get average of grades
- average = get_list_average(grades)
- print(average)
- # NEW FILE
- from functions import *
- show_vertical_text("hotel")
- # NEW FILE
- from functions import factorial
- # call the recursive function
- # (a function that calls itself repeatedly)
- result = factorial(6)
- print(result)
Add Comment
Please, Sign In to add comment