Foros del Web » Programando para Internet » Python »

Python while

Estas en el tema de Python while en el foro de Python en Foros del Web. Hola: Por fin estoy con los while, ya va siendo hora. Este PDF de Python enseña muy bien, aún así hay ejercicios que uno se ...
  #1 (permalink)  
Antiguo 12/10/2010, 22:00
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 16 años, 11 meses
Puntos: 8
Python while

Hola:

Por fin estoy con los while, ya va siendo hora. Este PDF de Python enseña muy bien, aún así hay ejercicios que uno se queda pescando...

Cita:
Hacer un programa que muestre todos los múltiplos de 6 entre 6 y 150, ambos inclusive.
Muchas veces no me deja claro que es lo que ha que hacer. Por lo que entiendo arriba, hay que hacer un bucle while. Haber como hay que hacer el múltiplo que piden.

Por ahora empecé así:

Código:
i = 1
while i <= 150:
    i *= 6
    print i
Cita:
6
36
216
Esto suena a raro, no creo que sea así lo que me está pidiendo.

Código:
i = 1
while i <= 150:
    i += 6
    print i
Cita:
7
13
19
25
31
37
43
49
55
61
67
73
79
85
91
97
103
109
115
121
127
133
139
145
151
Saludo.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar
  #2 (permalink)  
Antiguo 12/10/2010, 22:20
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Python while

Empieza desde i = 0 en tu segundo codigo:

Lo que haces es:
Código:
i = 1
while i <= 150:
    i += 6
    print i

#Si lo vemos en camara lenta o con un debbuger estas haciendo:
# i = 1
# 1 <= 150 verdadero
# i += 6
# i = i + 6
# i = 1 + 6
# i = 7
# ... así sucesivamente.
#cambiando i = 0 afuera del while debe corregir esto
Otro método sería por "fuerza bruta":
Código:
i = 1
while i <= 150:
    if i % 6 == 0: #Si i es exactamente divisible por 6 es multiplo
        print i
    i += 1
  #3 (permalink)  
Antiguo 12/10/2010, 22:23
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Python while

Si ya viste el ciclo for entonces seria mas fácil:
Código Python:
Ver original
  1. for i in range(6, 151, 6):
  2.     print i
  #4 (permalink)  
Antiguo 12/10/2010, 22:45
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 16 años, 11 meses
Puntos: 8
Respuesta: Python while

Sólo debo usar por el momento el while, los for para más adelante. jejeje.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:44.