Using while loop and If statements, print all the letters in the following string except for the letter ‘g’ and ‘o’. (5 marks)
‘goodbyemybaloon’
Hint: use continue keyword. Continue and Break are keywords to control the flow of a loop. Continue keyword returns the control to the beginning of the loop. Now try using the break keyword and see what happens. Expain your findings.
user_str = 'goodbyemybaloon'
i = 0
while True:
i += 1
if i > len(user_str)-1:
# the operator break exits the loop
break
if user_str[i] == 'g' or user_str[i] == 'o':
# skips subsequent actions if the condition is not met
continue
# printing characters on one line without a separator
print(user_str[i],end='',sep='')
Comments
Leave a comment