VISTA PREVIA: Ya sabemos cómo hacer una lista y acceder a cada elemento de esa lista usando un bucle.

Pero, ¿qué sucede si solo desea utilizar un solo elemento de la lista? Para hacer eso, usa un índice .

  • Hacer clic Ejecute y responda a la pregunta Preguntar. Vuelva a ejecutar el programa e intente con un número diferente.
  • Mire las líneas 19, 24, 29 y 34 en el editor de código. ¡Estas líneas usan índices, sobre los cuales aprenderá hoy!
  • Hacer clic Enviar y Siguiente para comenzar la lección de hoy!

To navigate the page using the TAB key, first press ESC to exit the code editor.

######################################################################## # PLACE LIST CREATIONS HERE # ######################################################################## stage_list = ["summer", "underwater", "flowers", "mars", "subway","soccerfield"] head_list = ["head1", "head2", "head3", "head4", "head5", "head6"] body_list = ["middle1", "middle2", "middle3", "middle4", "middle5", "middle6"] legs_list = ["legs1", "legs2", "legs3", "legs4", "legs5", "legs6"] ######################################################################## # PLACE ALL OTHER CODE BELOW THIS # ######################################################################## head_image = head_list[3] display_which_head = codesters.Text(head_image, 100, 83) head = codesters.Sprite(head_image, 0, 83) rand_number = random.randint(0,5) body_image = body_list[rand_number] display_which_body = codesters.Text(body_image, 100, 0) body = codesters.Sprite(body_image, 0, 0) rand_numb2 = random.randint(0,5) legs_image = legs_list[rand_numb2] display_which_legs = codesters.Text(legs_image, 100, -83) legs = codesters.Sprite(legs_image, 0, -83) stage_index = int(head.ask("Choose an index between 0 and 5.")) stage_image = stage_list[stage_index] stage.set_background(stage_image)
  • Run Code
  • Enviar Trabajo
  • Actividad Siguiente
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)