Answer to Question #200099 in Python for sudheer

Question #200099

def main():

  #Read string

  inputString = input()

  outputString=""

  for letter in inputString:

    number=getNumber(letter)

    if(number!=-1):

      outputString+=str(number)+"-"

    else:

      outputString+=letter



  print(outputString[:-1])

#This function convert letter to number

def getNumber(letter):

  alphabet = "abcdefghijklmnopqrstuvwxyz"

  for i in range(0,len(alphabet)):

    if letter.islower() and letter==alphabet[i]:

      return i+1

    if letter.isupper() and letter==alphabet[i].upper():

      return i+1

  return -1


main()

if we give python foundation we did not get the answer

what we get the answer is p-y-t-h-o-n- f-o-u-n-d-a-t-i-o-n


1
Expert's answer
2021-05-30T07:20:07-0400

two tips:

1. Functions should only do one simple job. You don't need to write a lot of code in the body of the function.

2. Whenever possible, always simplify your functions as shown below:

def getNumber2(letter):
    return "abcdefghijklmnopqrstuvwxyz".index(letter.lower())   

And if you work in a notebook sometimes kernel blocks user input if the last input was with interrupt. Just restart the kernel and all should work. And watch out for spaces in your code (there should be 4 after ':' )

Good luck!

def main(inputString):
    outputString=""
    for letter in inputString:
        number=getNumber(letter)
        if(number!=-1):
            outputString+=str(number)+"-"
        else:
            outputString+=letter
    print(outputString[:-1])


#This function convert letter to number


def getNumber(letter):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    for i in range(0,len(alphabet)):
        if letter.islower() and letter==alphabet[i]:
            return i+1
        if letter.isupper() and letter==alphabet[i].upper():
            return i+1
    return -1
    
main(input('enter string: '))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS