-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreakContinueCodingProblems.py
More file actions
67 lines (51 loc) · 1.69 KB
/
Copy pathBreakContinueCodingProblems.py
File metadata and controls
67 lines (51 loc) · 1.69 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
testString = "GhostTownOhio"
# for loop with the range() function
for i in range(0, len(testString),1):
print(testString[i], end="👻 ")
print("\r")
# for-each loop in Python
# eachChar represents the character in the current iteration
# as we iterate through the testString
for eachChar in testString:
print(eachChar, end="👻 ")
print("\r")
# Question 1 - Print every character in the testString except for letter "O" "o"
# USING THE FOR-EACH LOOP APPROACH AND CONTINUE STATEMENT
for eachLetter in testString:
if eachLetter == "O" or eachLetter == "o":
continue # To start a new iteration immediately
print(eachLetter, end=" ")
print("\r")
# Question 2 - To count the occurances of letter "O" "o" in the testString
# Use for each-loop, declare a new variable "count" to keep the count
count = 0
for eachLetter in testString:
if eachLetter == "O" or eachLetter == "o":
count = count + 1
print(count)
print("\r")
# Question 3 - To print every character before the second letter "O" or "o"
# Cut off at the second letter "O" or "o"
# for each loop, break, count variable
count = 0
for eachLetter in testString:
if eachLetter == "O" or eachLetter == "o":
count = count + 1
if count == 2:
break
print(eachLetter, end=" ")
print("\r")
# Question 4 - Reverse print all characters before the second letter of "O" or "o"
# Example
# testString = "GhostTownOhio"
# Output = T t s o h G
count = 0
temp_str = ""
for eachLetter in testString:
if eachLetter == "O" or eachLetter == "o":
count = count + 1
if count == 2:
break
# Add each letter into the temp-str
temp_str = temp_str + eachLetter
print(temp_str[::-1])