You are on page 1of 2

R Anterior Unidade 3 de 7 S Avançar T

" 100 XP

Exercício – Criar um loop 'While'


8 minutos

Este módulo requer uma área restrita para ser concluído.

Uma área restrita fornece acesso a recursos gratuitos. Não haverá cobranças na sua assinatura pessoal. A área
restrita poderá ser usada somente para concluir o treinamento no Microsoft Learn. O uso por qualquer outro motivo
é proibido e poderá levar à perda permanente do acesso à área restrita.

A Microsoft fornece essa experiência de laboratório e o conteúdo relacionado para fins educacionais. Todas as
informações apresentadas são de propriedade da Microsoft e destinam-se exclusivamente à aprendizagem sobre os
produtos e os serviços abordados neste módulo do Microsoft Learn.

Ativar área restrita

 Tempo de execução Arquivo Editar Exibir  Comentários

 Executar tudo   Kernel   Computação não conectado

    

Using while loops in Python


In Python, while loops let you run code an unknown number of times. The loops examine a Boolean condition and, as
long as the condition is true, the code inside the loop will run. This is very useful for situations like prompting a user for
values.

In this exercise, you're creating an application that prompts a user to enter a list of planets. In a later exercise, you'll add
code that displays the list. For now, though, you'll create only the code that prompts the user.

This exercise is broken into a series of steps. For each step you will be presented with the goal for the step, followed by
an empty cell. Enter your Python into the cell and run it. The solution for each step will follow each cell.

Start by adding two variables, one for the input from the user, named new_planet , and another variable for the list of
planets named planets

# Enter code below:

Your Python code should look like this:

new_planet = ''
planets = []
Create a while loop
Starting with the variables you've just created, create a while loop. The while loop will run while new_planet is not
set to done.

Inside the loop, check to see whether the new_planet variable contains a value, which should be the name of a planet.
This is a quick way to see whether the user has entered a value. If they have, your code will append that value to the
planets variable.

Complete the while loop by using input to prompt the user to enter a new planet name or done if they're done
# Enter
entering code below:
planet names. You'll store the value from input in the new_planet variable.
[2] 
Finally, outside of the while loop, print the list of planets by using print .

A l t thi t f th i tt ti t t bl l t d i t th t ti
Your code should look like this:

while new_planet.lower() != 'done':


if new_planet:
planets.append(new_planet)
new_planet = input('Enter a new planet, or done if done')

print(planets)

Desired output
When you run your notebook you will be prompted for new planet names until you enter done. (Enter a planet name
and select "Submit". For each new entry, overtype the previous entry and select "Submit".) Then you will be presented
with a list of the planets you entered.

 Sem computação Computação não conectado  Exibição Kernel não conectado

Unidade seguinte: Usar loops 'for' com listas

Continuar T

You might also like