Skip to content

Commit 52ee76a

Browse files
Caleb CurryCaleb Curry
authored andcommitted
more looping
1 parent a486b55 commit 52ee76a

1 file changed

Lines changed: 73 additions & 1 deletion

File tree

beginner_python/07-more-looping.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,76 @@
137137

138138
#Setting a starting value (such as -1), possibly changing it,
139139
#and checking it afterwards creates a flag variable.
140-
#Just a concept. Nothing new on syntax
140+
#Just a concept. Nothing new on syntax
141+
142+
######### "DO WHILE" loop ##########
143+
144+
#In other languages, there is a concept as a do-while loop.
145+
#These loops always execute at least once.
146+
#We can mimmick this in python easily.
147+
148+
i = 15
149+
print(i) #prints i atleast once no matter what
150+
i += 1
151+
while (i < 10):
152+
print(i)
153+
i += 1
154+
155+
156+
#To generalize this:
157+
158+
#do stuff
159+
#condition - true to continue
160+
#do stuff
161+
162+
#This structure is useful for sentinel / indefinate loops
163+
164+
165+
########## Indefinate / Sentinel loops ##########
166+
167+
#an Indefinate loop is a loop that we do not decide how long it will run ahead of time
168+
#The loop can be stopped, however. This makes it different than an infinate loop.
169+
#An example may be displaying a menu numerous times
170+
171+
172+
print("Do you want to continue? Y/N: ")
173+
response = input()
174+
while response == "Y" or response == "y":
175+
print("Do you want to continue? Y/N: ")
176+
response = input()
177+
#a logical name for the variable would be 'continue' or 'in'
178+
#however these are keyword. don't try it.
179+
180+
#not super common vocab but good to know...
181+
#A sentinel value is a value used to stop a loop. In this case it is anything besides "Y" or "y"
182+
#for programs a sentinel value is often 'q'
183+
184+
185+
########## UPPER AND LOWER ##########
186+
187+
188+
#We can also write our code like so
189+
print("Do you want to continue AGAIN? Y/N: ")
190+
response = input()
191+
while response.lower() == "y":
192+
print("Do you want to continue? Y/N: ")
193+
response = input()
194+
195+
#This is important to understand as "Y" and "y" are not the same thing
196+
#Overlooking this can introduce logical bugs in our software
197+
198+
#There is also an upper.
199+
#We can also invoke it on constant strings
200+
print("am i screaming?".upper())
201+
202+
########## checking if a string is uppercase or lowercase ##########
203+
204+
name = "Caleb"
205+
if name.isupper():
206+
print("Upper")
207+
elif name.islower():
208+
print("Lower")
209+
else:
210+
print("Mixed")
211+
212+
#not sure when you might need this but still good to know.

0 commit comments

Comments
 (0)