Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # functions.py, this contains
- # the definitions of all our own functions
- # Consider this as a toolkit!
- # define a function show_text()
- # => only shows some text to user
- def show_text():
- print("Welcome to our app!")
- print("-------------------")
- print("Please follow instructions.")
- print()
- # another function: combine_text
- # three params: first name, surname, 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 = 2023
- 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 them
- 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 a string
- def reverse_string(text):
- return text[::-1]
- # define a function that checks
- # if given text is palindrome or not
- # (text is same also reversed)
- def check_palindrome(text):
- reversed_text = reverse_string(text)
- # check if text is same both ways, return a boolean
- if text == reversed_text:
- return True
- else:
- return False
- # define a function that checks is given
- # order code follows 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 the result, whatever it is at this point
- return result
- # define a function that
- # calculates the average of
- # any numeric list
- def get_list_average(numbers):
- total = sum(numbers)
- amount = len(numbers)
- result = total / amount
- result = round(result, 2)
- return result
- # ACTUAL CODE FILES AFTER THIS
- from functions import *
- # call our function
- show_text()
- # print empty line
- print()
- # call our own function with parameters
- combine_text("John", "Doe", 31)
- print("App stopped!")
- # NEW FILE
- from functions import *
- # call our own function
- # this function returns data
- # => we need to save it to a variable
- # compare to round() -function
- year = get_year()
- print(year)
- print()
- # NEW FILE
- from functions import *
- # ask integer from user
- user_number = input("Give a number:\n")
- user_number = int(user_number)
- # test our function with a value
- result = get_even_number_text(user_number)
- # print the returned value
- print(result)
- # NEW FILE
- from functions import *
- # let's call our helper function
- # that reverses the text (and hides the complex Python process)
- text = reverse_string("what about this sentence")
- print(text)
- # NEW FILE
- from functions import *
- hours = input("Give amount of hours:\n")
- hours = int(hours)
- # use our own function to count amount of days in these hours
- days = hours_to_days(hours)
- print(f"{days} days in total.")
- # NEW FILE
- from functions import *
- word = input("Give a sentence:\n")
- # use our own function to see if this is palindrome or not
- # result will be a boolean : False/True
- result = check_palindrome(word)
- if result:
- print("Palindrome!")
- else:
- print("Not a palindrome.")
- # NEW FILE
- from functions import *
- order = input("Give a order code:\n")
- # use our own function to check if code
- # is of expected format: False/True
- result = check_order(order)
- if result:
- print("Order code OK!")
- else:
- print("Invalid order code.")
- # NEW FILE
- from functions import *
- # define our number lists
- numbers = [5, 7, 9, 8, 5, 6, 7, 4, 9, 10, 9]
- grades = [1, 5, 4, 5, 3, 2, 3, 4, 1, 4, 5]
- temperatures = [-8.5, -9.7, -6.5, -24.1, -20.7, -11.6]
- # get average for numbers
- avg_numbers = get_list_average(numbers)
- print(avg_numbers)
- # get average for grades
- avg_grades = get_list_average(grades)
- print(avg_grades)
- # get average for temperatures
- avg_temperatures = get_list_average(temperatures)
- print(avg_temperatures)
Advertisement
Add Comment
Please, Sign In to add comment