-
Notifications
You must be signed in to change notification settings - Fork 68
/
basic_functions.py
95 lines (69 loc) · 2.76 KB
/
basic_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Purpose: Illustrate basic functions in Python.
Author: Denise Case
This file name is: basic_functions.py
This module name is: basic_functions
Functions
Functions are reusable bits of code.
Functions are defined using the def keyword.
Functions are called using the function name and parentheses.
Functions can accept parameters (arguments).
Functions can return values.
Built-in Functions
Python has many helpful built-in functions.
len() is a built-in function that returns the length of an object.
min() is a built-in function that returns the smallest item in an iterable.
max() is a built-in function that returns the largest item in an iterable.
print() is a built-in function that prints to the screen.
input() is a built-in function that gets input from the user.
str() is a built-in function that converts a value to a string.
int() is a built-in function that converts a value to an integer.
@ uses webbrowser module to open a web browser to a url
"""
# ----------------- INSTRUCTOR GENERATED CODE -----------------
# Use this handy logger to document your work automatically
# import setup_logger function from instructor-generated module
from util_logger import setup_logger
# setup the logger using the current file name (a built-in variable)
logger, logname = setup_logger(__file__)
# ----------------- END INSTRUCTOR GENERATED CODE -----------------
# Import from Python Standard Library
import webbrowser
# Declare Variables
url = "https://docs.python.org/3/library/functions.html"
number_list = [1, 2, 3, 4, 5]
# Use built-in functions like len(), min(), max()
length = len(number_list)
smallest = min(number_list)
largest = max(number_list)
hint = "HINT: In the terminal, hit up arrow to rerun a command and try again.\n"
# Log Information
logger.info(f"Functions url = {url}")
logger.info(f"number_list = {number_list}")
logger.info(f"len(number_list) = {length}")
logger.info(f"min(number_list) = {smallest}")
logger.info(f"max(number_list) = {largest}")
# Print an empty line to the terminal
print()
# Greet the user
print("Greetings!")
# Get the user's name and greet them
name = input("What's your name? (type your name and hit enter): ")
message = f"Hello {name.capitalize()}!"
print(message)
print()
logger.info(f"message = {message}")
# Ask the user if they want to see built-in functions
response = input("Would you like to see all the built-in functions? (y/n) ").lower()
logger.info(f"response = {response}")
print(f"You said {response}!")
print(f"{hint}")
if response == "y":
print()
print("Let's open a web page. Python makes it easy!")
webbrowser.open_new(url)
print()
print("There's a lot of built-in functions ready to use!")
print("We'll learn more about them later.")
print()
# TODO: Run with different responses n, y, other...