Question

In: Computer Science

Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...

  1. Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’.
  2. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new python program with name "main.py" is created, which contains following code.

main.py :

def CommonLetters(mystring):
numletters=0 #declaring variable count
for letter in mystring: #using for checking each letter from mystring
if letter == 'P': #checking letter P
numletters = numletters + 1 #increment value of count
elif letter=='y': #checking letter y
numletters = numletters + 1 #increment value of count
elif letter=='t': #checking letter t
numletters = numletters + 1 #increment value of count
elif letter=='h': #checking letter h
numletters = numletters + 1 #increment value of count
elif letter=='o': #checking letter o
numletters = numletters + 1 #increment value of count
elif letter=='n': #checking letter n
numletters = numletters + 1 #increment value of count
return numletters #return number of letters   
  
#declaring variable to store boolean value
flag=True
#using while to ask user string
while flag:
#asking user to enter string
mystring=input("Enter String : ")
if mystring!="" : #if mystring is not empty
#calling method and passing parameter as mystring
print("Number of letters : ",CommonLetters(mystring))
else:
flag=False; #used to break the loop
  

*******************************

Screen used for Indentation :

======================================================

Output : Compile and Run main.py to get the screen as shown below

Screen 1 :main.py

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT