-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbucles.py
More file actions
95 lines (68 loc) · 2.14 KB
/
bucles.py
File metadata and controls
95 lines (68 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Sentencia While: Mientras se cumpla una condición, ejecuta una instrucción
NOTA: Incrementar el contador para evitar que este en un bucle infinito
"""
# want_greet = "S"
# while want_greet.upper() == "S":
# print("¡Hola! que tal")
# want_greet = input("¿Quieres saludar otra vez? [S/N]: ")
# print("¡Adiós! que tengas un buen día 👋")
# print("*** Romper un bucle while: Break ***")
# MAX_GREETS = 4
# num_greets = 0
# want_greet = "S"
# while want_greet.upper() == "S":
# print("¡Hola! Te hablo desde la computadora")
# num_greets += 1
# if num_greets == MAX_GREETS:
# print(f"Has alcanzado el máximo número de saludos: {MAX_GREETS}")
# break
# want_greet = input("¿Quieres saludar otra vez? [S/N]: ")
# else:
# print("Usted no quiere más saludos 🤪")
# print("¡BYE! 👋")
# print("*** Continuar un bucle while: Continue ***")
# Saltar delante hacia la siguiente repetición del bucle
# want_greet = "S"
# valid_option = 0
# while want_greet == "S":
# print("¡HOLA! CÓMO ESTÁS ?")
# want_greet = input("¿Quieres un saludo de nuevo? [S/N]: ")
# if want_greet not in "SN":
# print("No te entiendo, pero te saludo de nuevo...")
# want_greet = "S"
# continue
# valid_option += 1
# print(f"{valid_option} respuestas válidas")
# print("¡ADIÓS! que tengas un buen día 👋")
"""
Bucle for: Permite iterar strings, lists, tuples, dictionaries, ficheros, etc.
"""
word = "python"
for letter in word:
print(letter)
# Romper un bucle for con break
print("ROMPIENDO UN BUCLE FOR:")
word = "Python"
for letter in word:
if letter == "t":
break
print(letter)
print("SECUENCY OF NUMBERS ⬇")
# for i in range(10):
# print(i)
# for i in range(1, 10):
# print(i)
for i in range(1, 7, 3):
print(i)
# Usando el guión bajo: _ -> Repetir una acción un número de veces.
for _ in range(15):
print("Repeat me 15 times")
"""
Bucles anidados:
"""
print("BUCLES ANIDADOS:")
for num_table in range(1, 10):
for mul_factor in range(1, 10):
result = num_table * mul_factor
print(f"{num_table} x {mul_factor} = {result}")