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
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: '))
Comments
Leave a comment