Python - atorado en ejercicio

 
Vista:

atorado en ejercicio

Publicado por Angel (7 intervenciones) el 05/07/2019 04:34:18
Hola todos,

apenas estoy aprendiendo un poco de phyton y en un curso que estoy tomando esoty atorado con este ejercicio:

el set es:
def test() : # do not change this line!
values = [4, 7, 8, 9, -5] # do not change this line!

# write your code here so that it modifies values

# be sure to indent your code!

print(values)


y las instrucciones son:

1. See if the last element of the list is negative. If so, remove it from the list.
2. Then, calculate the average of the first two elements of the list and insert it in between
those two elements.
3. Last, swap the values of the first and last elements of the list.
At the end of the program, print out the list using “print(values)”. In this case, it should print [9,
5.5, 7, 8, 4]

¿alguien que me pudiera dar un pequeña ayuda, no sé como hacer el paso 1 y 2? y por más que he probado con remove and if me marca error.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 765
Bronce
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

atorado en ejercicio

Publicado por dario (185 intervenciones) el 05/07/2019 05:32:17
Hola, te dejo esto por si te ayuda.
Salu2.
Nota: De que pagina estas sacando estos ejercicios, quiero hacerlos tambien.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def test() : # do not change this line!
  values = [4, 7, 8, 9, -5] # do not change this line!
  media = 0
 
  #Punto 1
  if values[-1] < 0 :
    values.remove(values[-1])
 
  #Punto 2
  media = (values[0] + values[1]) / 2
  print(media)
  #Insertar la media
  values.insert(1,media)
 
  print(values)
 
#Main
test()
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

atorado en ejercicio

Publicado por angel (7 intervenciones) el 05/07/2019 06:08:43
Muchas gracias. Oye pero al momento de hacer el tres me marca
NameError: name 'swampPositions' is not defined

posterior al paso dos agregue
Values= [4, 7, 8, 9, -5]
pos1, pos2 = 0, 4

¿que me recomiendas?

Y estos ejercicios los estoy siguiendo de un curso en coursera: computational thinking problem solving.

Gracias por tu apoyo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 765
Bronce
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

atorado en ejercicio

Publicado por dario (185 intervenciones) el 05/07/2019 10:47:56
Hola, te dejo en ejercicio completo
salu2.
Estoy usando Python3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def test() : # do not change this line!
  values = [4, 7, 8, 9, -5] # do not change this line!
  media = 0
 
  #Punto 1
  if values[-1] < 0 :
    values.remove(values[-1])
 
  #Punto 2
  media = (values[0] + values[1]) / 2
  print(media)
 
  #Insertar la media
  values.insert(1,media)
 
  #Swap
  aux = values[0]
  values[0] = values[-1]
  values[-1] = aux
 
  print(values)
 
#Main
test()
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar

atorado en ejercicio

Publicado por Angel (7 intervenciones) el 05/07/2019 16:06:03
Muchas gracias, de plano si andaba en otro lado.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar