Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python: Can someone explain me this code, line by line. I know the answers but,

ID: 3684702 • Letter: P

Question

Python:

Can someone explain me this code, line by line. I know the answers but, I just don't understand how

a)

scary = 'boo! boo!'
place = 0
while place <= len(scary):
current = scary[place]
print(current, end='')
place += scary.count(current)

b)

eap = 'rapping, rapping'
print(eap[:4] + eap[-4:])

c)

seasonal = 'boo'
out = ''
idx = 0
while True:
letter = seasonal[idx]
letterCount = seasonal.count(letter)
out += letter
if seasonal[idx] > seasonal[idx+1]:
idx += letterCount
elif seasonal[idx] < seasonal[idx+1]:
idx -= letterCount
else:
break
print(out)

d)

favs = ['Leia', 'R2-D2', 'Solo', 'Chewbacca']
vowels = 'aeiou'
for vowel in vowels:
for fav in favs:
if vowel in fav:
continue
else:
print(fav[0], end='')
break

e)

def fileCount(fileName, s):
inF = open(fileName)
count = 0
content = inF.read()
for thing in content:
if s in thing:
count += 1
inF.close()
return count

w = open('seuss.txt', 'w')
w.write('You are on your own' + ' ')
w.write('and you know what you know' + ' ')
w.close()
print(fileCount('seuss.txt', 'you'))

#I know the answer is 0 but, I thought it is '1' ,PLEASE EXPLAIN THANK YOU

Sorry for the indentation but, chegg does not allow indentation

Explanation / Answer

Here is the explanation in comments for you:

#!/usr/bin/python
#a)
scary = 'boo! boo!'   #The string is assigned to the variable scary.
place = 0           #The value 0 is assigned to the variable place.
while place <= len(scary):   #For each character in the variable scary.
current = scary[place]       #Assign the character to current.
print current, ' ',       #Print it followed by a space.
place += scary.count(current)   #Increment the place value by the number of times, current character appears in the variable scary.
#So initially, The first character, i.e., the one at index 0 will be printed, and as b appears twice in the list, the place will be incremented by 2,
#Then, the character at position 2, i.e., o will be printed. As o appears 4 times, the place value is now incremented to 6.
#Then, the character at position 6, i.e., o will be printed. As o appears 4 times, the place value is now incremented to 10.
#As 10 is not less than or equal to length of the variable, you'll come out of the loop.
#Therefore, the output is: b o o
#b)
eap = 'rapping, rapping'   #The string is assigned to the variable eap.
print(eap[:4] + eap[-4:])   #Prints the first 4 characters, and the last 4 characters of the string.
#Therefore the output is: rappping. Here, rapp are the first 4 characters, and ping are the last 4 characters.
#c)
seasonal = 'boo'   #The string is assigned to the variable seasonal
out = ''           #The empty string is assigned to the variable out.
idx = 0               #The value 0 is assigned to the variable idx.
while True:           #Runs in an infinite loop.
letter = seasonal[idx]   #letter is assigned the character in the string seasonal at position idx.
letterCount = seasonal.count(letter)   #letterCount is assigned a value the number of times letter appears in the variable seasonal.
out += letter       #The character in letter is appended to out.
if seasonal[idx] > seasonal[idx+1]:   #if the adjacent character in seasonal, are in ascending order, starting from idx,
idx += letterCount       #Increment the value of idx, by letterCount.
elif seasonal[idx] < seasonal[idx+1]:   #if the adjacent character in seasonal, are in descending order, starting from idx,
idx -= letterCount       #Decrement the value of idx, by letterCount.
else:                   #Else, i.e, if both the adjacent characters are the same.
break                   #Stop running the loop, and come out of it.
print(out)               #Print the string out.
#So, initially, the first character, i.e., b is assigned to letter. And letterCount is 1, as b appears only once in the string.
#The character b is appended to out. And b is less than o, So, idx will be decremented by letterCount, So, idx value is -1.
#letter = o, And letterCount is 2, as o appears twice in the string. The character o is appended to out. o is greater than b, and
#and therefore, idx is incremented by 2, and will now become 1. letter = o, And letterCount = 2, as o appears twice in the string.
#The character o is appended to out. o is equal to o, and therefore, you'll come out of the loop.
#Therefore, the output is: boo.
#d)
favs = ['Leia', 'R2-D2', 'Solo', 'Chewbacca']   #The list is assigned to the variable favs.
vowels = 'aeiou'       #The string is assigned to the variable vowels.
for vowel in vowels:   #For each character in vowels.
for fav in favs:       #For each string in favs.
if vowel in fav:       #If that vowel is in the current string.
continue               #move on to the next string.
else:                   #If not,
print(fav[0], ' ')   #Print the first character in fav, to the screen.
break               #Come out of the inner loop.
#For the first character in vowel, a, is in Leia, move to next string, a is not in the second string.
#So, print the first character R, and break the loop.
#Now for the second character in vowel, e, is in Leia, move to next string, e is not in the second string.
#So, print the first character R, and break the loop.
#Now for the third character in vowel, i, is in leia, move to next string, i is not in the second string.
#So, print the first character R, and break the loop.
#Now the fourth character o is not in the first string.
#So, print the first character L, and break the loop.
#Now the fifth character u is not in the first string.
#So, print the first character L, and break the loop. Stop execution.
#Finally, the output is: R R R L L.