computação i - python aula 7 - teórica: estrutura de ... · estrutura de repetic¸˜ao while...
TRANSCRIPT
Computacao I - Python
Aula 7 - Teorica: Estrutura de Repeticao com Teste de
Parada: while
Joao C. P. da Silva
Carla A. D. M. Delgado
Ana Luisa Duboc
Dept. Ciencia da Computacao - UFRJ
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 1 / 17
Estrutura de Repeticao while
Permite que o programador especifique que a funcao deve repetir umconjunto de comandos enquanto uma dada condicao for verdadeira.
while <condicao>:<sequencia de comandos>
Exemplo
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 2 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
1 > 0
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
1 > 0 ⇒ True
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
1 > 0 ⇒ True
listax=[’x’,’x’]
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
1 > 0 ⇒ True
listax=[’x’,’x’]numero = 0
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
1 > 0 ⇒ True
listax=[’x’,’x’]numero = 0
0 > 0
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
1 > 0 ⇒ True
listax=[’x’,’x’]numero = 0
0 > 0 ⇒ False
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
1 de f exemplo1 ( numero ) :2 ””” Funcao que c r i a uma l i s t a formada por s t r i n g s ’ x ’ . A quant i dade de s t r i n g s ’ x ’ e
i g u a l a numero .3 Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t a x = [ ]7 wh i l e numero > 0 :8 l i s t . append ( l i s t a x , ’ x ’ )9 numero = numero − 1
10 r e t u r n l i s t a x
exemplo1(2)
listax=[]
2 > 0 ⇒ True
listax=[’x’]numero = 1
1 > 0 ⇒ True
listax=[’x’,’x’]numero = 0
0 > 0 ⇒ False
return [’x’,’x’]
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 3 / 17
Estrutura de Repeticao while
while <condicao>:<sequencia de comandos>
A <condicao> e uma expressao ou dado do tipo booleano (True ou False), talcomo os testes usados com o comando IF.
Estrutura tambem conhecida como laco de repeticao ou “loop”: o bloco decomandos e sequencialmente repetido tantas vezes quanto o teste da condicao forverdadeiro.
Somente quando a condicao se torna falsa a proxima instrucao apos o bloco decomandos associado ao while e executada (fim do laco).
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 4 / 17
Estrutura de Repeticao while
while <condicao>:<sequencia de comandos>
Se a <condicao> da estrutura while ja for falsa desde o inıcio, o bloco de<sequencia de comandos> associado a ela nunca e executado.
Deve haver algum processo dentro do bloco de <sequencia de comandos> quetorne a condicao falsa e a repeticao seja encerrada, ou um erro GRAVE ocorrera:sua funcao ficara rodando para sempre!!
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 5 / 17
Estrutura de Repeticao while
1 de f e xemp l owh i l e 0 ( numero ) :23 ””” Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t anum = [ ]7 wh i l e numero > 0 :8 l i s t anum [ numero−1] = numero9 numero = numero −1
10 r e t u r n l i s t anum
Qual o problema desta funcao?
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 6 / 17
Estrutura de Repeticao while
1 de f e xemp l owh i l e 0 ( numero ) :23 ””” Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t anum = [ ]7 wh i l e numero > 0 :8 l i s t anum [ numero−1] = numero9 numero = numero −1
10 r e t u r n l i s t anum
Qual o problema desta funcao?
1 de f e xemp l owh i l e 1 ( numero ) :23 ””” Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t anum = numero ∗ [ 0 ]7 wh i l e numero > 0 :8 l i s t anum [ numero−1] = numero9 numero = numero −1
10 r e t u r n l i s t anum
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 6 / 17
Estrutura de Repeticao while
1 de f e xemp l owh i l e 0 ( numero ) :23 ””” Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t anum = [ ]7 wh i l e numero > 0 :8 l i s t anum [ numero−1] = numero9 numero = numero −1
10 r e t u r n l i s t anum
Qual o problema desta funcao?
1 de f e xemp l owh i l e 2 ( numero ) :23 ””” Parametro de ent r ada : i n t4 Va l o r de Retorno : l i s t ”””56 l i s t anum = [ ]7 wh i l e numero > 0 :8 l i s t . i n s e r t ( l i s t anum , 0 , numero )9 numero = numero −1
10 r e t u r n l i s t anum
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 7 / 17
Estrutura de Repeticao while
Exemplo
1 de f exemplo2 ( numero ) :23 ””” Funcao que conta quantas ve z e s s e pode r e d u z i r em 1 o v a l o r do numero passado como
parametro a t e chega r a z e ro .4 Parametro de ent r ada : i n t5 Va l o r de r e t o r n o : s t r ”””67 contador = 0 # v a r i a v e l c on tadora8 wh i l e numero > 0 :9 numero = numero − 1
10 contador = contador + 111 r e t u r n ”A funcao rodou ” + s t r ( c ontador ) + ” ve ze s . ”
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 8 / 17
Estrutura de Repeticao while
Faca uma funcao que determina a soma de todos os numeros pares desde100 ate 200.
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 9 / 17
Estrutura de Repeticao while
Faca uma funcao que determina a soma de todos os numeros pares desde100 ate 200.
1 de f somaPares ( ) :23 ””” Funcao que c a l c u l a a soma dos numeros pa r e s de 100 a 2004 Parametro de ent r ada : nao tem5 Va l o r de r e t o r n o : i n t ”””67 soma = 0 # v a r i a v e l acumuladora8 contador = 100 # o contador nao p r e c i s a comecar de ze ro9 wh i l e c ontador <= 200 :
10 soma = soma + contador11 contador = contador + 2 # o contador nao p r e c i s a i r de 1 em 112 r e t u r n soma
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 9 / 17
Estrutura de Repeticao while
A funcao abaixo apresenta algum problema?
1 de f exemplo3 ( ) :23 ””” Parametro de ent r ada : nao tem4 Va l o r de r e t o r n o : i n t ”””56 x = 107 wh i l e x > 8 :8 x = x+ 29 r e t u r n x
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 10 / 17
Estrutura de Repeticao while
A funcao abaixo apresenta algum problema?
1 de f exemplo3 ( ) :23 ””” Parametro de ent r ada : nao tem4 Va l o r de r e t o r n o : i n t ”””56 x = 107 wh i l e x > 8 :8 x = x+ 29 r e t u r n x
Sendo X igual a 10, o teste X > 8 e inicialmente verdadeiro.
Enquanto a condicao for verdadeira, apenas o comando X = X + 2sera executado. Porem incrementar a variavel X nao altera a validadeda condicao X > 8.
Logo, a repeticao segue indefinidamente! (Loop infinito)
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 10 / 17
Estrutura de Repeticao while
O que faz a seguinte funcao ?
1 de f soma ( numero ) :23 ””” Parametro de ent r ada : i n t4 Va l o r de r e t o r n o : i n t ”””56 soma = 07 contador = 08 wh i l e c ontador < numero :9 soma = soma + contador
10 contador = contador + 111 r e t u r n soma
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 11 / 17
Estrutura de Repeticao while
Faca uma funcao que gere numeros aleatorios entre 1 e 10 e calcule a soma destes numeros ateque seja gerado o numero 5.Use a funcao randint(inicio,fim) do modulo random para gerar um numero aleatorio, onde osvalores de (inıcio,fim) representam o intervalo desejado para os numeros a serem gerados.Exemplo: randint(1,10) → gera um numero aleatorio entre 1 e 10, inclusive.
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 12 / 17
Estrutura de Repeticao while
Faca uma funcao que gere numeros aleatorios entre 1 e 10 e calcule a soma destes numeros ateque seja gerado o numero 5.Use a funcao randint(inicio,fim) do modulo random para gerar um numero aleatorio, onde osvalores de (inıcio,fim) representam o intervalo desejado para os numeros a serem gerados.Exemplo: randint(1,10) → gera um numero aleatorio entre 1 e 10, inclusive.
1 from random import r a nd i n t23 de f somaA l e a to r i a ( ) :45 ””” Parametro de ent r ada : nao tem6 Va l o r de r e t o r n o : i n t ”””78 soma = 09 numero = r and i n t ( 1 , 10)
10 wh i l e numero != 5 :11 soma = soma + numero12 numero = r and i n t ( 1 , 10)13 r e t u r n soma
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 12 / 17
Estrutura de Repeticao while
Faca uma funcao que gere numeros aleatorios entre 1 e 10 e calcule a soma destes numeros ateque seja gerado o numero 5.Use a funcao randint(inicio,fim) do modulo random para gerar um numero aleatorio, onde osvalores de (inıcio,fim) representam o intervalo desejado para os numeros a serem gerados.Exemplo: randint(1,10) → gera um numero aleatorio entre 1 e 10, inclusive.
Solucao usando listas.
1 from random import r a nd i n t23 de f somaA l e a to r i a ( ) :45 ””” Parametro de ent r ada : nao tem6 Va l o r de r e t o r n o : i n t ”””78 l i s t a = [ ]9 numero = r and i n t ( 1 , 10)
10 wh i l e numero != 5 :11 l i s t . append ( l i s t a , numero )12 numero = r and i n t ( 1 , 10)13 r e t u r n sum ( l i s t a )
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 13 / 17
Estrutura de Repeticao while
Faca uma funcao que some 10 numeros gerados aleatoriamente no intervalo de 1 a 5.
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 14 / 17
Estrutura de Repeticao while
Faca uma funcao que some 10 numeros gerados aleatoriamente no intervalo de 1 a 5.
1 from random import r a nd i n t23 de f soma10 ( ) :45 ””” Parametro de ent r ada : nao tem6 Va l o r de r e t o r n o : i n t ”””78 soma = 09 contador = 0
10 wh i l e c ontador < 10 :11 numero = r and i n t ( 1 , 5 )12 soma = soma + numero13 contador = contador + 114 r e t u r n soma
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 14 / 17
Estrutura de Repeticao while
Para cada um dos itens abaixo, faca uma tabela mostrando os valores que i , j e n assumemdepois de cada execucao do laco while.
def ...i = 0j = 10n = 0while i < j:
i = i + 1j = j - 1n = n + 1
def ...i = 0j = 0n = 0while i < 10:
i = i + 1n = n + i + jj = j + 1
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 15 / 17
Estrutura de Repeticao while
Faca uma funcao que dada uma lista de tamanho desconhecido contendoas notas de uma turma de alunos, retorne a media dessas notas.
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 16 / 17
Autores
Joao C. P. da Silva Lattes
Carla Delgado Lattes
Ana Luisa Duboc Lattes
Colaboradores
Anamaria Martins Moreira Lattes
Fabio Mascarenhas Lattes
Leonardo de Oliveira Carvalho Lattes
Charles Figueiredo de Barros Lattes
Fabrıcio Firmino de Faria Lattes
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 17 / 17
Computacao I - Python
Aula 7 - Teorica: Estrutura de Repeticao com Teste de
Parada: while
Joao C. P. da Silva
Carla A. D. M. Delgado
Ana Luisa Duboc
Dept. Ciencia da Computacao - UFRJ
http://www.dcc.ufrj.br/˜pythonUFRJ/ Computacao I - Python Aula 7 18 / 17