Introducci贸n

1

El Zen de Python

Conjuntos

2

Sets

3

Modificando conjuntos

4

Operaciones con conjuntos

5

Playgrounds: Elimina elementos duplicados usando conjuntos

Comprehensions

6

List Comprehension

7

Dictionary Comprehension

8

Dictionary Comprehension: condition

9

Playgrounds: Crea una lista usando List Comprehension

10

Lists vs. Tuples vs. Sets

Funciones

11

Funciones

12

Funciones: return

13

Par谩metros por defecto y m煤ltiples returns

14

El scope

15

Refactor game

16

Playgrounds: Tienda de Tecnolog铆a

17

Funciones an贸nimas: lambda

18

Higher order function: una funci贸n dentro de otra funci贸n

19

Map

20

Map con diccionarios

21

Reto: map con inmutabilidad

22

Playgrounds: Multiplica todos los elementos por dos

23

Filter

24

Playgrounds: Retorna solo palabras de 4 letras y m谩s

25

Reduce

M贸dulos

26

M贸dulos

27

Mis propios m贸dulos

28

M贸dulos como scripts: __name__ y __main__

29

Paquetes

30

Playgrounds: Calcular la suma de todas las compras

Manipulaci贸n de archivos y errores

31

Iterables

32

Errores en Python

33

Manejo de excepciones

34

Playgrounds: Captura la excepci贸n: ZeroDivisionError

35

Leer un archivo de texto

36

Escribir en un archivo

37

Leer un CSV

38

Playgrounds: Lee un CSV para calcular el total de gastos

Gr谩ficas en Python

39

Creando una gr谩fica

40

Reto: graficando la poblaci贸n de un pa铆s

41

Reto: graficando la poblaci贸n mundial

Pr贸ximos pasos

42

隆Conoce otros proyectos de este curso!

43

Reconoce tu proceso y certif铆cate

44

Toma el Curso de PIP y Entornos Virtuales con Python

No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Playgrounds: Calcular la suma de todas las compras

30/44

Aportes 213

Preguntas 6

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

Buenas tardes! Yo realic茅 el c贸digo de la siguiente manera:

import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
    
  totals = my_functions.get_totals(orders)
  Sum = my_functions.calc_total(totals)
  return Sum

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Yo lo hice sin necesidad de llamar a las funciones:

Me gusta mucho la dificultad de losplaygrounds OwO

import my_functions

def get_total(orders):
  
  ordenes = my_functions.get_totals(orders)

  
  suma_ordenes = my_functions.calc_total(ordenes)
  
  return suma_ordenes

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

from my_functions import * para no nombrar el archivo a la hora de usar las funciones

main:

from my_functions import get_totals, calc_total

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_totals(orders)
result = calc_total(total)
print(result)

my_functions:

def get_totals(orders):
   return [order['total'] for order in orders]

def calc_total(totals):
   return sum(totals)

En mi caso yo hice el import directamente dentro de la funcion get_total

Pueden hacer el **import **dentro de la funci贸n para cumplir con la indicaci贸n de " # Tu c贸digo aqu铆 馃憞"

Mi soluci贸n usando las buenas pr谩cticas del ENTRY POINT : if name == 鈥main

import my_functions

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

def get_total(orders):
  totals = my_functions.get_totals(orders)
  return my_functions.calc_total(totals)

def run():
  total = get_total(orders)
  print(total)

if __name__ == '__main__':
  run()

Se complican mucho algunos

import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  return my_functions.calc_total(my_functions.get_totals(orders))

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Si no indicas el from, marca un error.

from my_functions import get_totals, calc_total

def get_total(orders):


  result = get_totals(orders)
  total_order = calc_total(result)
  return total_order

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Aqui la respuesta
Creo que no deberia ser tan lineal al momento de aceptar la respuesta. Intent茅 varios metodos los cuales cumplian con lo que pedia el reto y me la marcaba como m谩l.

import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  prices = my_functions.get_totals(orders)
  sum_prices = my_functions.calc_total(prices)
  return sum_prices

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

#Solucion en 3 lineas

ojo:

Esta no es la mejor solucion, sino la m谩s resumida porque para el campo profesional necesitamos que otras personas entiendan nuestro codigo.

from my_functions import get_totals, calc_total

def get_total(orders):
  return calc_total(get_totals(orders))
  • A mi opinion esta esta mucho mejor en cuanto a lo esto se refiere:
def get_total(orders):
  total_items_arr = get_totals(orders)
  return calc_total(total_items_arr)

Una versi贸n super resumida seria:

import my_functions

def get_total(orders):
  return my_functions.calc_total(my_functions.get_totals(orders))

Answer

import my_functions
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  total=my_functions.get_totals(orders)
  
  return my_functions.calc_total(total)


def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
 def get_total(orders):
  # Extraer el valor total de cada 贸rden de compra
  totals = map(lambda x: x['total'], orders)
  
  # Sumar todos los valores totales y retornar el resultado
  return sum(totals)
  
orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Comparto mi soluci贸n:

import my_functions

def get_total(orders):
    obtain = my_functions.get_totals(orders)
    summary = my_functions.calc_total(obtain)
    return summary


orders = [
    {
        "customer_name": "Nicolas",
        "total": 100,
        "delivered": True,
    },
    {
        "customer_name": "Zulema",
        "total": 120,
        "delivered": False,
    },
    {
        "customer_name": "Santiago",
        "total": 20,
        "delivered": False,
    }
]

total = get_total(orders)
print(total)

Mi soluci贸n鈥

import my_functions #importamos el m贸dulo de funciones

def get_total(orders):
  totales=my_functions.get_totals(orders)
  return my_functions.calc_total(totales)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Hola! Yo lo resolv铆 as铆:

import my_functions
def get_total(orders):
  return my_functions.calc_total(my_functions.get_totals(orders))

yo solo escrib铆 una l矛nea de c贸digo aparte de las importaciones, que por cierto prefer铆 importar espec铆ficamente solo para ser un poco mas explicito, analizando los m贸dulos que importe me di cuenta que solamente tenia que dar a 鈥渃alc_total鈥 de input el output de 鈥済et_totals鈥 al que le di de input el dataset 鈥渙rders鈥 siendo estrictos ni siquiera escribi codigo, solo conecte las entradas con las salidas

from my_functions import calc_total
from my_functions import get_totals
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  return calc_total(get_totals(orders))

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Ac谩 mi respuesta:

import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = my_functions.get_totals(orders)
  
  return my_functions.calc_total(totals)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
import my_functions 

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  total = my_functions.get_totals(orders)
  suma_total = my_functions.calc_total(total)
  return suma_total

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

main

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  from my_functions import get_totals, calc_total
  totales = get_totals(orders)
  sumas = calc_total(totales)

  return sumas

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

my_functions

def get_totals(orders):
   return [order['total'] for order in orders]

def calc_total(totals):
   return sum(totals)

Solo fue agregar dos lineas

Main

import my_functions  #Importar el modulo

def get_total(orders):
  result = my_functions.calc_total(my_functions.get_totals(orders)) #Hacer que calculara todo usando las dos funciones
  return result

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Aun asi no se si esta del todo bien

El playground no funciona

import my_functions
def get_total(orders):

Tu c贸digo aqu铆 馃憞

list_totales = my_functions.get_totals(orders)
total = my_functions.calc_total(list_totales)
return total

orders = [
{
鈥渃ustomer_name鈥: 鈥淣icolas鈥,
鈥渢otal鈥: 100,
鈥渄elivered鈥: True,
},
{
鈥渃ustomer_name鈥: 鈥淶ulema鈥,
鈥渢otal鈥: 120,
鈥渄elivered鈥: False,
},
{
鈥渃ustomer_name鈥: 鈥淪antiago鈥,
鈥渢otal鈥: 20,
鈥渄elivered鈥: False,
}
]

total = get_total(orders)
print(total)

Intent茅 resolver el reto de esta forma y aunque el resultado est茅 bien, me dice que est谩 mal 馃槮

import my_functions


orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = my_functions.calc_total(my_functions.get_totals(orders))
print(total)
\#main.py def get\_total(orders): result= list(map(lambda item: item\['total'], orders)) return sum(result) \#archivo de ejecuci贸n: from main import get\_total orders = \[ { "customer\_name": "Nicolas", "total": 100, "delivered": True, }, { "customer\_name": "Zulema", "total": 120, "delivered": False, }, { "customer\_name": "Santiago", "total": 20, "delivered": False, } ] total = get\_total(orders) print(total)
Mi soluci贸n fue solo escribir la l铆nea 1 y la l铆nea 5: ```js from my_functions import get_totals, calc_total def get_total(orders): # Tu c贸digo aqu铆 馃憞 return calc_total(get_totals(orders)) ```
```python def get_total(orders): # Tu c贸digo aqu铆 馃憞 suma_total = sum(order["total"] for order in orders) return suma_total ```
1. import my\_functionsdef get\_total(orders):聽 # Tu c贸digo aqu铆 馃憞聽 totals = my\_functions.get\_totals(orders)聽 print(totals)聽 Sum = my\_functions.calc\_total(totals)聽 return Sum orders = \[聽 {聽 聽 "customer\_name": "Nicolas",聽 聽 "total": 100,聽 聽 "delivered": True,聽 },聽 {聽 聽 "customer\_name": "Zulema",聽 聽 "total": 120,聽 聽 "delivered": False,聽 },聽 {聽 聽 "customer\_name": "Santiago",聽 聽 "total": 20,聽 聽 "delivered": False,聽 }] total = get\_total(orders)print(total)```js ```
```js import functools def get_total(orders): # Tu c贸digo aqu铆 馃憞 prices = list(map(lambda item: item['total'], orders)) result = functools.reduce(lambda conter, item: conter + item, prices) return result orders = [ { "customer_name": "Nicolas", "total": 100, "delivered": True, }, { "customer_name": "Zulema", "total": 120, "delivered": False, }, { "customer_name": "Santiago", "total": 20, "delivered": False, } ] total = get_total(orders) print(total) ```import functoolsdef get\_total(orders):聽 # Tu c贸digo aqu铆 馃憞聽 prices = list(map(lambda item: item\['total'], orders))聽 result = functools.reduce(lambda conter, item: conter + item, prices)聽 return result orders = \[聽 {聽 聽 "customer\_name": "Nicolas",聽 聽 "total": 100,聽 聽 "delivered": True,聽 },聽 {聽 聽 "customer\_name": "Zulema",聽 聽 "total": 120,聽 聽 "delivered": False,聽 },聽 {聽 聽 "customer\_name": "Santiago",聽 聽 "total": 20,聽 聽 "delivered": False,聽 }] total = get\_total(orders)print(total)
```js def get_total(orders): # Tu c贸digo aqu铆 馃憞 from my_functions import get_totals, calc_total result = calc_total(get_totals(orders)) return result orders = [ { "customer_name": "Nicolas", "total": 100, "delivered": True, }, { "customer_name": "Zulema", "total": 120, "delivered": False, }, { "customer_name": "Santiago", "total": 20, "delivered": False, } ] total = get_total(orders) print(total) ```
```js https://platzi.com/new-home/clases/4260-python-funciones/57451-playgrounds-calcular-la-suma-de-todas-las-compras/ ```
![](https://platzi.com/new-home/clases/4260-python-funciones/57451-playgrounds-calcular-la-suma-de-todas-las-compras/) ```js ```
```js from my_functions import get_totals def get_total(orders): # Tu c贸digo aqu铆 馃憞 totals = get_totals(orders) return sum(totals) orders = [ { "customer_name": "Nicolas", "total": 100, "delivered": True, }, { "customer_name": "Zulema", "total": 120, "delivered": False, }, { "customer_name": "Santiago", "total": 20, "delivered": False, } ] total = get_total(orders) print(total) ```
Yo lo resolv铆 as铆! ```js from my_functions import get_totals def get_total(orders): # Tu c贸digo aqu铆 馃憞 totals = get_totals(orders) return sum(totals) orders = [ { "customer_name": "Nicolas", "total": 100, "delivered": True, }, { "customer_name": "Zulema", "total": 120, "delivered": False, }, { "customer_name": "Santiago", "total": 20, "delivered": False, } ] total = get_total(orders) print(total) ```
Yo lo realice de una forma mas resumidaimport my\_functions ```js import my_functions def get_total(orders): # Tu c贸digo aqu铆 馃憞 return my_functions.calc_total(my_functions.get_totals(orders)) orders = [ { "customer_name": "Nicolas", "total": 100, "delivered": True, }, { "customer_name": "Zulema", "total": 120, "delivered": False, }, { "customer_name": "Santiago", "total": 20, "delivered": False, } ] total = get_total(orders) print(total) ```

excelente reto鈥

import my_functions
def get_total(orders):
    total=my_functions.get_totals(orders)
    return sum (total)
Para resolverlo yo lo hice as铆: ![](https://static.platzi.com/media/user_upload/image-9c2ba5b0-a01f-41a4-8542-68be59f3fd1f.jpg)El if que esta en la segunda funci贸n es para el caso de lista vac铆a, si se da la suma seria 0. Ya en el main solo importe las funciones dentro de la funci贸n. ![](https://static.platzi.com/media/user_upload/image-0ffcf853-e2e1-46f1-82e3-63eb6df8c72e.jpg)Para terminar solo ejecute las dos ultimas lineas: total = get\_total(orders) print(total)
![](https://static.platzi.com/media/user_upload/image-1dc1d2ae-51ed-48dd-9a69-24d03ac737cd.jpg)```js ```

Yo lo hice de esta manera

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  import my_functions

  totals = my_functions.get_totals(orders)

  return my_functions.calc_total(totals)
  

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Me gusto este ejercicio cada vez siento que estoy mejorando.

from my_functions import get_totals, calc_total

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = get_totals(orders)
  orders = calc_total(totals)
  return orders

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total) 
Aunque el ejercicio dice que hay que usar los m贸dulos, hice esta l贸gica, otra manera de resolver el ejercicio: ![](https://static.platzi.com/media/user_upload/msedge_3f07MfUDJb-dc15e2e7-28fa-4cc3-8db7-706b31e9491a.jpg)
![](https://static.platzi.com/media/user_upload/msedge_bB92fxxqe8-8a86fa67-8678-498a-8678-7de8166dac7d.jpg)
```js from my_functions import get_totals,calc_total def get_total(orders): total=calc_total(get_totals(orders)) return total ```
Hola encontr茅 una forma m谩s simplificada de resolver el desaf铆o empleando las funtiontools mas espec铆ficamente REDUCE y una funci贸n lambda```python def get_totals(orders): return functools.reduce(lambda total, item: total + item['total'], orders, 0) #Modulo main.py from my_functions import get_totals orders = [ { "customer_name": "Nicolas", "total": 100, "delivered": True, }, { "customer_name": "Zulema", "total": 120, "delivered": False, }, { "customer_name": "Santiago", "total": 20, "delivered": False, } ] total = get_totals(orders) print(total). ```
Quiza no sea lo m谩s optimo, porque considero q no se entiende tan bien `import my_functionsdef get_total(orders):聽 # Tu c贸digo aqu铆 馃憞聽 return my_functions.calc_total(my_functions.get_totals(orders))` `orders = [聽 {聽 聽 "customer_name": "Nicolas",聽 聽 "total": 100,聽 聽 "delivered": True,聽 },聽 {聽 聽 "customer_name": "Zulema",聽 聽 "total": 120,聽 聽 "delivered": False,聽 },聽 {聽 聽 "customer_name": "Santiago",聽 聽 "total": 20,聽 聽 "delivered": False,聽 }]` `total = get_total(orders)print(total)`
```python import my_functions def get_total(orders): totals = my_functions.get_totals(orders) sumatoria = my_functions.calc_total(totals) return sumatoria orders = [ { "customer_name": "Nicolas", "total": 100, "delivered": True, }, { "customer_name": "Zulema", "total": 120, "delivered": False, }, { "customer_name": "Santiago", "total": 20, "delivered": False, } ] total = get_total(orders) print(total) ```import my\_functions def get\_total(orders):聽 totals = my\_functions.get\_totals(orders)聽 sumatoria = my\_functions.calc\_total(totals)聽 return sumatoria orders = \[聽 {聽 聽 "customer\_name": "Nicolas",聽 聽 "total": 100,聽 聽 "delivered": True,聽 },聽 {聽 聽 "customer\_name": "Zulema",聽 聽 "total": 120,聽 聽 "delivered": False,聽 },聽 {聽 聽 "customer\_name": "Santiago",聽 聽 "total": 20,聽 聽 "delivered": False,聽 }] total = get\_total(orders)print(total)
```js import my_functions def get_total(orders): totals = my_functions.get_totals(orders) sumatoria = my_functions.calc_total(totals) return sumatoria ```import my\_functions def get\_total(orders):聽 totals = my\_functions.get\_totals(orders)聽 sumatoria = my\_functions.calc\_total(totals)聽 return sumatoria
Buen d铆a, En mi caso modifique my\_functions.py main.py ```python import my_functions def get_total(orders): return my_functions.suma(orders) ```my\_functions.py ```js def suma(orders): prices = list(map(lambda item: item['total'], orders)) return sum(prices) ```
  1. Crea un archivo my_functions.py que contenga las funciones get_totals y calc_total. En este archivo, get_totals toma una lista de 贸rdenes de compra y devuelve una lista con los totales de cada orden, mientras que calc_total toma una lista de totales y devuelve la suma de esos totales.
pythonCopy code
# my_functions.py

def get_totals(orders):
   return [order['total'] for order in orders]

def calc_total(totals):
   return sum(totals)

  1. En el archivo main.py, importa las funciones del archivo my_functions.py. Luego, en la funci贸n get_total, llama a get_totals para obtener una lista de totales y luego pasa esa lista a calc_total para obtener la suma total.
pythonCopy code
# main.py
from my_functions import get_totals, calc_total

def get_total(orders):
    totals = get_totals(orders)
    return calc_total(totals)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

  1. Ahora puedes ejecutar main.py, que utilizar谩 las funciones definidas en my_functions.py para calcular y mostrar la suma total de todas las 贸rdenes de compra.
![](https://static.platzi.com/media/user_upload/Screenshot%202023-09-27%20at%2020.42.49-3cc1c676-342f-460c-88b1-5decad1fbf44.jpg)
import my_functions
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  return my_functions.calc_total(my_functions.get_totals(orders))

Esta es una posibilidad resumida

def get_total (orders): 
	import my_functions as mf
	return mf.calc_total(mf.get_totals(orders))
	
import my_functions
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = my_functions.get_totals(orders)
  total = my_functions.calc_total(totals)
  return total

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
import my_functions
def get_total(orders):
  totals=my_functions.get_totals(orders)
  return my_functions.calc_total(totals)

import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  ordersList = my_functions.get_totals(orders)
  totalAmount = my_functions.calc_total(ordersList)
  totalAmount=int(totalAmount)
  return totalAmount

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]


total = get_total(orders)
print(total)

Me cost贸, tuve que acudir a las preguntas y m谩s bien ah铆 le铆 una respuesta a mi duda.
Para hacer funcionar en Replit, he creado un archivo my_functions.py al que copi茅 las funciones y funcion贸 correctamente.

from my_functions import get_totals, calc_total

def get_total(orders):
  orders = get_totals(orders)
  totals = calc_total(orders)
  return totals
orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Comparto mi soluci贸n con comentarios en el c贸digo.

  • Abajo

  • M谩s abajo

  • Aqu铆 =>

Mi solucion

import my_functions as mf
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = mf.get_totals(orders)
  return mf.calc_total(totals) 

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞

  totals = my_functions.get_totals(orders)
  total = my_functions.calc_total(totals)

  return total

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)```

Good later, It鈥檚 my answer of this exercise:

from my_functions import get_totals, calc_total

def get_total(orders):
  totalm=get_totals(orders)
  return calc_total(totalm)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
from my_functions import get_totals, calc_total

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  return calc_total(get_totals(orders))

Mi aporte es:

from my_functions import get_totals, calc_total

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  result_get_totals = get_totals(orders)
  
  return calc_total(result_get_totals)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Buen di, aqu铆 el mio.

<def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  total1 =0
  for i in orders:
    total1+= i["total"]
  return total1

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)> 

Podemos importar las funciones de un archivo en una sola l铆nea de comando, para no llamar my_functions.etc鈥

from my_functions import get_totals, calc_total

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = get_totals(orders)
  total = calc_total(totals)
  return total

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Mi respuesta

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  import my_functions as lol
  answer = lol.calc_total(lol.get_totals(orders))
  return answer

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
# aqui lo importante es que tenemos una carpeta que serian nuestros paquetes que contiene modulos los cuales contienen las funciones

from paquetes.modulo_1 import obtener_totales
from paquetes.modulo_2 import calcular_total

def obtener_total(ordenes_de_compra):
  totales = obtener_totales(ordenes_de_compra)
  return calcular_total(totales)

ordenes_de_compra = [
  {
    "nombre_de_cliente": "Nicolas",
    "total": 100,
    "entregado": True,
  },
  {
    "nombre_de_cliente": "Zulema",
    "total": 120,
    "entregado": False,
  },
  {
    "nombre_de_cliente": "Santiago",
    "total": 20,
    "entregado": False,
  }
]

total = obtener_total(ordenes_de_compra)
print(total) # 240 y se pudo ejecutar como script desde python 30_ordenes_de_compra.py

# la mejor practica es usando name space (nombres reservados) y creamos __init.py__ por si se ejecuta desde versiones anterioes a la 3.3 y desde ahi importamos
import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = my_functions.get_totals(orders)
  total = my_functions.calc_total(totals)
  return total

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Este es mi aporte chicos.

b谩sicamente hice que el result atrapara los datos de el diccionario (exactamente los valores), luego la variable result_2 los suma y se muestran . espero les sirva de gu铆a

import my_functions
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  result=my_functions.get_totals(orders)
  result_2=my_functions.calc_total(result)
  return result_2

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)


.
.
.
.
.
.
.

def get_total(orders):
  import my_functions

  total = my_functions.get_totals(orders)

  return sum(total)

De lo pocos que he hecho solo, bueno con ayuda de las notas de las clases, pero sin ver las soluciones de otros 5 + 5 = Yes!!!

Si el nombre del archivo es largo se puede usar un 鈥榓lias鈥. Yo us茅 鈥榤y鈥

import my_functions as my
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = my.get_totals(orders)
  return my.calc_total(totals)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  orders = my_functions.get_totals(orders)
  orders = my_functions.calc_total(orders)
  return orders
  

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
from my_functions import *

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  return calc_total(get_totals(orders))
 

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  list_total_orders = my_functions.get_totals(orders)
  total_orders = my_functions.calc_total(list_total_orders)
  return total_orders

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Mi solucion al playground 馃槃

from my_functions import get_totals, calc_total

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  return calc_total(get_totals(orders))

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

import my_functions

def get_total(orders):

Tu c贸digo aqu铆 馃憞

get_orders = my_functions.get_totals(orders)
sum_orders = my_functions.calc_total(get_orders)
return sum_orders

Aqu铆 como resolv铆 el reto:

import my_functions
def get_total(orders):
  suma = my_functions.get_totals(orders)
  return my_functions.calc_total(suma)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Buen d铆a
Esta es mi propuesta de soluci贸n.

import my_functions as mf

def get_total(orders):
  total = mf.calc_total(mf.get_totals(orders))
  return total

Esta fue mi solucion al ejercicio presentado, espero sirva de guia

import my_functions
def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totales = my_functions.get_totals(orders)
  result = my_functions.calc_total(totales)
  return result

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Comparto por si les llega a servir!

import my_functions

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  result = my_functions.get_totals(orders)
  suma = my_functions.calc_total(result)
  return suma

Esta es la solution que he implementado

rom my_functions import get_totals, calc_total


orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_totals(orders)
print(calc_total(total))

Gracias profe, el concepto en practica

al principio pens茅 usar reduce, pero cumpliendo con el reto 馃挭馃悕

<import my_functions as mf

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  totals = mf.get_totals(orders)
  return mf.calc_total(totals)

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)> 

Hola, les comparto mi Soluci贸n

import my_functions

def get_total(orders):

return my_functions.calc_total(my_functions.get_totals(orders))

orders = [
{
鈥渃ustomer_name鈥: 鈥淣icolas鈥,
鈥渢otal鈥: 100,
鈥渄elivered鈥: True,
},
{
鈥渃ustomer_name鈥: 鈥淶ulema鈥,
鈥渢otal鈥: 120,
鈥渄elivered鈥: False,
},
{
鈥渃ustomer_name鈥: 鈥淪antiago鈥,
鈥渢otal鈥: 20,
鈥渄elivered鈥: False,
}
]

total = get_total(orders)
print(total)

Gratamente sorprendido por la velocidad de respuesta de esta version del playground. Enhorabuena!

def get_total(orders):
  # Tu c贸digo aqu铆 馃憞
  import my_functions
  list_totales= my_functions.get_totals(orders)
  suma_total = my_functions.calc_total(list_totales)
  return suma_total

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
from my_functions import calc_total, get_totals

def get_total(orders):
  return calc_total(get_totals(orders))
def get_totals(orders):
	return [order['total'] for order in orders]

def calc_total(totals):
	import functools
	total = functools.reduce(lambda counter, item: counter + item, totals)
return total
  
import my_functions

def get_total(orders):
  price_list = my_functions.get_totals(orders)
  price_list_add = my_functions.calc_total(price_list)
  return price_list_add

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)
from my_functions import get_totals, calc_total

def get_total(orders):
  return calc_total(get_totals(orders))

orders = [
  {
    "customer_name": "Nicolas",
    "total": 100,
    "delivered": True,
  },
  {
    "customer_name": "Zulema",
    "total": 120,
    "delivered": False,
  },
  {
    "customer_name": "Santiago",
    "total": 20,
    "delivered": False,
  }
]

total = get_total(orders)
print(total)

Buenas, ac谩 comparto mi soluci贸n por si le sirve a alguien. 馃榿 ![](

cost贸 un poquito pero entendiendo bien el archivo my_function.py y ejecutando funci贸n por funci贸n se puede entender.

import my_functions

def get_total(orders):

Tu c贸digo aqu铆 馃憞

toral_2 = my_functions.get_totals(orders)
return my_functions.calc_total(total_2)

orders = [
{
鈥渃ustomer_name鈥: 鈥淣icolas鈥,
鈥渢otal鈥: 100,
鈥渄elivered鈥: True,
},
{
鈥渃ustomer_name鈥: 鈥淶ulema鈥,
鈥渢otal鈥: 120,
鈥渄elivered鈥: False,
},
{
鈥渃ustomer_name鈥: 鈥淪antiago鈥,
鈥渢otal鈥: 20,
鈥渄elivered鈥: False,
}
]

total = get_total(orders)
print(total)

undefined