I have a final exam coming up and this is the only question I am unable to answer correctly:
Question 4
word = 'off' prev = "" for letter in word: if prev == ""; prev = letter out = "" continue elif letter == prev: prev = letter out = letter break else: prev = letter out = word[0] print(out)
a) o
b) f
c) of
d) "" (the empty string)
e) none of the above
When I was trying to solve it I thought the answer was going to be "none of the above" because out
is defined in local variable scope and would no longer exist out of each part of the if
statement. When I ran the code in the compiler to check my answer I found that the correct answer is b. Can someone explain why the local variable scope allows this answer to be correct? Thank you in advance.