#Can someone help me put some comments in every line to help understand what is
ID: 3698473 • Letter: #
Question
#Can someone help me put some comments in every line to help understand what is going on.
def doOperations(a, b):
c = "abcdefghijklmnopqrstuvwxyz"
d = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
e = "AEIOU"
f = "aeiou"
if a == 'A':
print(len(b), "characters")
elif a == 'B':
count = 0
for i in range(len(b)):
if b[i] in c or b[i] in d:
count = count + 1
print(count, "letters")
elif a == 'C':
count = 0
for i in range(len(b)):
if b[i] in e or b[i] in f:
count = count + 1
print(count, "vowels")
elif a == 'D':
found = 1
i = 0
j = len(b) - 1
while i < j:
if b[i] != b[j] :
found = 0
break
i = i + 1
j = j - 1
if found == 1:
print("It is a palindrome")
else:
print("It is not a palindrome")
elif a == 'E':
ct = ""
for i in range(len(b)):
for j in range(len(c)):
if c[j] == b[i]:
j = j - 3
if j < 0:
j = 26 + j
ct = ct + c[j]
for j in range(len(d)):
if d[j] == b[i]:
j = j - 3
if j < 0:
j = 26 + j
ct = ct + d[j]
print(ct)
def printMenu():
print("A.How many characters are in the string?")
print("B.How many letters are in the string?")
print("C.How many vowels are in the string?")
print("D.Is the string a palidrome?")
print("E.What is the Cesar Cipher(shift 3) of the string")
while True:
inp = input("Enter a string :")
printMenu()
response = input("Your choice?")
if response == 'Q':
break
doOperations(response, inp)
Explanation / Answer
def doOperations(a, b):
#initializes c with alphabets in smaller case
c = "abcdefghijklmnopqrstuvwxyz"
#initializes d with alphabets in upper case
d = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#initializes e with vowels in upper case
e = "AEIOU"
#initializes f with vowels in smaller case
f = "aeiou"
#Checks if user selection is A
if a == 'A':
# prints the lenght. len() returns lenght of the string
print(len(b), "characters")
#Checks if user selection is B
elif a == 'B':
#Initialized count with 0
count = 0
#Loop thru all the character os input string and counts
#len() return leng and range() returns values 0,1,2..lenght
for i in range(len(b)):
#To count number of letters each character of input string verified against c and d . in operator return true if available
if b[i] in c or b[i] in d:
count = count + 1
print(count, "letters")
#Checks if user selection is C
elif a == 'C':
count = 0
#Loop thru all the character os input string
#len() return leng and range() returns values 0,1,2..lenght
for i in range(len(b)):
#To count number of vovels each character of input string verified against e and f . in operator return true if available
if b[i] in e or b[i] in f:
count = count + 1
print(count, "vowels")
#Checks if user selection is D
elif a == 'D':
found = 1
#initialize 0 to access staring position of string
i = 0
#initialize lenght-1 to access end of string
j = len(b) - 1
while i < j:
#Compare if first and last character is same and repeat for second and last but one and so on
if b[i] != b[j] :
# if any of the pair not same then set 0
found = 0
break
i = i + 1
j = j - 1
# flag used to print its palindrome or not
if found == 1:
print("It is a palindrome")
else:
print("It is not a palindrome")
##Checks if user selection is E
elif a == 'E':
ct = ""
#Loop thru all the character os input string
#len() return leng and range() returns values 0,1,2..lenght
for i in range(len(b)):
#Shift three characters before i.e. if input has V then print s
for j in range(len(c)):
if c[j] == b[i]:
j = j - 3
if j < 0:
j = 26 + j
ct = ct + c[j]
for j in range(len(d)):
if d[j] == b[i]:
j = j - 3
if j < 0:
j = 26 + j
ct = ct + d[j]
print(ct)
#Function to display menu
def printMenu():
print("A.How many characters are in the string?")
print("B.How many letters are in the string?")
print("C.How many vowels are in the string?")
print("D.Is the string a palidrome?")
print("E.What is the Cesar Cipher(shift 3) of the string")
#infinite loop until user selects Q
while True:
# read user input
inp = input("Enter a string :")
# function call to print menu
printMenu()
# user input to selected option
response = input("Your choice?")
# quit
if response == 'Q':
break
# funcion call to do all the work
doOperations(response, inp)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.