-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtutorial_25.py
51 lines (41 loc) · 1.16 KB
/
tutorial_25.py
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
names = ['jake', 'kate', 'alice', 'miller', 'bob']
grades = [80, 60, 46, 79, 96]
# example 1 - for each loop
for name in names:
print(name + ', you are invited to my party.')
# example 2 - indexed loop
for index in range(0, len(names), 2):
print(names[index] + ', you are invited to my party.')
# example 3 - testing range function outputs
test = range(2,4)
print('That range function would return:', list(test))
# example 4 - using else
test_char = 'e'
for char in 'Jeremy':
if char == test_char:
print('yes')
break
else:
print('no')
# example 5 - use a copy of your list if you are editing it
team = names
for person in team.copy():
if person == 'kate':
team.append('nick')
print(team)
# example 6 - loop through multiple lists
student_grades = dict()
for n, g in zip(names, grades):
student_grades.update({n:g})
print(student_grades)
# example 7 - exiting a program
users = names
for tries in reversed(range(3)):
current_user = input('Enter in your name: ')
if current_user in users:
break
else:
print('Incorrect. Tries left =', tries)
else:
raise SystemExit
print('Welcome,', current_user)