-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions fun.py
More file actions
56 lines (39 loc) · 1.14 KB
/
functions fun.py
File metadata and controls
56 lines (39 loc) · 1.14 KB
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
# basic of functions. to use call the function
def age_label(age):
label = "my age " + age
return label
results = age_label("22")
print(results)
# functions that start with a boolean often start with a name is
def is_freezing(temp):
return temperature < 0
# give good names
def sum_score(score, bonus):
print(score + bonus)
sum_score(100,50)
# the parameter (cart here) in the condition (function) we pass when calling the function -> here 80
def add_shipping(cart):
if cart < 100:
print(f"Total: {cart + 10}") #returns Total: 90
add_shipping(80)
# of course you can nest code in functions
def calculator(operator, x, y):
if operator == "+":
print(x+y)
elif operator == "-":
print(x - y)
else:
print(f"unknown: {operator}")
calculator("-", 30, 20)
# while loops with functions
def onboard_passenger(bookings):
counter = 1
while counter <= bookings:
print(f"Passenger {counter} added.")
counter += 1
onboard_passenger(4)
# for loops
def display_progress(total_files):
for i in range(total_files):
print(f"File {i} out of {total_files}")
display_progress(3)