-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFun with for-loops.py
More file actions
96 lines (72 loc) · 2.63 KB
/
Fun with for-loops.py
File metadata and controls
96 lines (72 loc) · 2.63 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
96
# Create a for-loop that breaks when i==10.
nums=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
for i in nums:
if i==10:
print(f'{i}: I found number "{i}" ')
break
print(i)
'''----------------------------------------------------------------'''
# Create a for-loop that continues on when i==10.
for i in nums:
if i==10:
print(f'{i}: I found number "{i}" ')
continue
print(i)
'''----------------------------------------------------------------'''
# Create a for-loop, using an 'input' statement that
# allows the user to input the number of the for-loop.
# If a number doesn't exist, the for-loop breaks.
while True:
try:
enter_num=int(input('Enter any number up to 20 and I will find it. '))
for i in nums:
if i==enter_num:
print(f'{i}: I found number "{i}" ')
break
elif enter_num<1:
print(f'Sorry! I cannot find "{enter_num}" ')
break
elif enter_num>20:
print(f'Sorry! I cannot find "{enter_num}" ')
break
print(i)
break
except ValueError:
print('Sorry! Numbers only please.')
'''----------------------------------------------------------------'''
# Create a right triangle shape with a for-loop, using a
# start value of 1.
for i in range(1,21):
print('* '*i,i)
'''----------------------------------------------------------------'''
# Create a right triangle shape with a for-loop, using a
# start value of 1 and a step value of 2.
for i in range(1,21,2):
print('* '*i,i)
'''----------------------------------------------------------------'''
# Create a for-loop, using an 'input' statement that
# allows the user to input the number of the for-loop.
while True:
try:
enter_num=int(input('Please enter a number, or numbers '))
for i in range(1,enter_num+1):
print('* '*i,i)
break
except ValueError:
print(f'Sorry! Numbers only please.')
'''----------------------------------------------------------------'''
# Loop through a tuple using a for-loop.
tuple_loop=(
'One','Two',
'Three','Four',
'Five','Six',
'Seven','Eight',
'Nine','Ten'
)
for i in tuple_loop:
print(i,end=' ')
'''----------------------------------------------------------------'''
# Loop through a tuple using a for-loop, along with
# a 'print' statement message 'I am number'
for i in tuple_loop:
print('I am number '+i+'.')