-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathfor_loops.py
104 lines (63 loc) · 3.44 KB
/
for_loops.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
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
97
98
99
100
101
102
103
104
#********************************************************************
#
# Team Edge List Mini-project: FOR LOOP CHALLENGES
#
# Complete the following loop challenges below. Follow the ToDos
# 1. COUNTER: Write a loop that prints a happy birthday message for every
# year you have been alive.
# 2. ITERATOR: Write a for for loop that prints every item in a list
# 3. EVEN COUNTDOWN: Write a for loop that counts down from 100 to 0,
# printing only the odd numers
# 4. FINDER: Write a function that takes in a list and a word and prints
# CONGRATULATIONS!! if the word is found in the list
# 5. NESTED: Write a function that logs every letter in a sentence
#
# ***************************************************************/
import random
print("------------------- CHALLENGE 1 : COUNTER -------------------")
#this list prints every number between 0 and 10, using range
for x in range(11):
print("Counter at: " + str(x))
#-->TODO: Write a loop that prints a happy birthday message for every year you have been alive.
print("------------------- CHALLENGE 2 : ITERATOR ----------------------")
#here is a list full of colors...
colors = ['red' , 'violet' , 'cyan' , 'pink' , 'lime' , 'white' , 'yellow', 'black' , 'magenta', 'green', 'orange']
#This is how you can iterate through a list:
for x in colors:
print("The color is: " + x)
#-->TODO: Declare a list with at least 10 animals. You provide the animals.
animals = []
#-->TODO: Print all the animals in the array with a for loop.
print("------------------- CHALLENGE 3 : EVEN COUNTDOWN ------------------")
#This makes a random number between 0-50
random = random.randint(0, 50)
#this if/else statement checks if the number is even using the modulo operator (%)
if random % 2 == 0:
print(str(random) + " is even!")
else:
print(str(random) + " is odd!")
#-->TODO: Write a function that counts BACKWARDS from 100 and prints only odd numbers
#-->TODO: Write a function that counts BACKWARDS from the given random number and prints only even numbers
print("------------------- CHALLENGE 4 : Finder ------------------")
#This code uses the in operator to see if an element exists in a list. It only has to appear once.
color = input('Type a one word color and see if it is one of my favorite colors! >> ')
if color in colors:
print("Yes, that color is a fav")
else:
print("No, that color is not one my favorites")
#-->TODO Declare a list of any strings you want: cities, friends, movies, etc.
#-->TODO Prompt the user to "Guess" if an element is present. Store their response in a variable
#-->TODO Write function to prompt the user and see if the element is present. If so, print CONGRATULATIONS!
#-->TODO Call your function.
print("------------------- CHALLENGE 5 : Nested ------------------")
#this is how you get the length of a word:
big_word = "antidisestablishmentarianism"
print(f"{big_word} has {len(big_word)} letters")
#this is how you can nest for loops, one insde the other! These loop through all the colors, and then through all the characters in the color
for color in colors:
#for all the colors:
print(color)
for c in color:
print(" - " + c) #log each letter. Remember, a string is also an array of characters.
#-->TODO Write a function that prints every letter in a sentence that a user enters.
#-->CHALLENGE: Let the user know which word is the shortest one!