Repository navigation
Expand file tree
/
Copy pathfunctions_v2.py
More file actions
85 lines (61 loc) · 2.99 KB
/
Copy pathfunctions_v2.py
File metadata and controls
85 lines (61 loc) · 2.99 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Example script showing definition and usage of functions in python.
For the article Funktioner, parametrar och returvärden version 2.
"""
blt_ingredients = ["ost", "bacon", "sallad", "majonnäs"]
blt_recipe = [
"stek bacon i en stekpanna", "smöra en sida av brödskivan, börja sätta ihop sandwichen",
"stek sandwichen med medelvärme i ca 3-5 minuter", "avsluta med att lägga på majonäsen"
]
blt_ingredients_string = ", ".join(blt_ingredients[:-1]) + " och " + blt_ingredients[-1]
blt_recipe_string = ", ".join(blt_recipe[:-1]) + " och " + blt_recipe[-1]
print("En BLT innehåller " + blt_ingredients_string + ".")
print("För den perfekta BLT-mackan skall dessa steg följas: " + blt_recipe_string + ".")
def print_sandwich_string(ingredients, presentation):
"""
Formats ingredience and recipe lists in a representable string.
"""
formated_string = ", ".join(ingredients[:-1]) + " och " + ingredients[-1] + "."
print(presentation + formated_string)
print_sandwich_string(blt_ingredients, "En BLT innehåller ")
print_sandwich_string(blt_recipe, "För den perfekta BLT-mackan skall dessa steg följas: ")
grilled_cheese_ingredients = ["vitlökssmör", "riven ost"]
print_sandwich_string(grilled_cheese_ingredients, "En Grilled Cheese innehåller ")
def print_sandwich(ingredients, presentation="Prova vår nya sandwich som innehåller "):
"""
Formats ingredience and recipe lists in a representable string.
"""
formated_string = ", ".join(ingredients[:-1]) + " och " + ingredients[-1] + "."
print(presentation + formated_string)
print_sandwich(blt_ingredients, "En BLT innehåller ")
unknown_ingredients = ["sallad", "tonfiskröra"]
print_sandwich(unknown_ingredients)
def create_sandwich_string(ingredients, presentation="Prova vår sandwich som innehåller "):
"""
Formats ingredience and recipe lists in a representable string.
"""
number_of_ingredients = len(ingredients)
if number_of_ingredients == 1:
result = presentation + ingredients[0] + "."
return result
formated_string = ", ".join(ingredients[:-1]) + " och " + ingredients[-1] + "."
result = presentation + formated_string
return result
blt_ingredients_string = create_sandwich_string(blt_ingredients, "En BLT innehåller ")
print(blt_ingredients_string)
def formatted_sandwich_string(ingredients, presentation="Prova vår sandwich som innehåller"):
"""
Formats ingredience and recipe lists in a representable string.
"""
number_of_ingredients = len(ingredients)
if number_of_ingredients == 1:
return "{} {}.".format(presentation, ingredients[0])
return "{presentation} {comma_sepearated_elements} och {last_element}.".format(
presentation=presentation,
comma_sepearated_elements=", ".join(ingredients[:-1]),
last_element=ingredients[-1]
)
blt_ingredients_string = formatted_sandwich_string(blt_ingredients, "En BLT innehåller")
print(blt_ingredients_string)