73.3k views
10 votes
Write a function charInWord that takes in two parameters, a char (character) and a word (string). The program will return true if the character is in the word and false if it is not. If word is not a string type, or if char is not a string type, or if the length of char is greater than 1, the function should return None. Your main program should call the function and print char is in word if the function returns true, or char is not in word if the function returns false, using the user-supplied values instead of char and word. The program should print incorrect input provided if the function returns None. Ex: If the input is: a cat the output is: a is in cat Ex: If the input is: a club the output is: a is not in club Ex: If the input is: ab horse the output is:

User Rubs
by
5.1k points

1 Answer

12 votes

Answer:

The program in Python, is as follows:

def charInWord(chr,word):

if len(chr)>1:

print("None")

elif not(chr.isalpha() and word.isalpha()):

print("None")

else:

if word.find(chr) == -1:

print(chr+" is not in "+word)

else:

print(chr+" is in "+word)

chr = input("Character: ")

word = input("Word: ")

print(charInWord(chr,word))

Step-by-step explanation:

This defines the function

def charInWord(chr,word):

This checks if the length of character is greater than 1

if len(chr)>1:

If yes, it prints None

print("None")

This checks if the character or the word contains invalid character

elif not(chr.isalpha() and word.isalpha()):

If yes, it prints None

print("None")

This is executed for valid parameters

else:

If the character is not present, this is executed

if word.find(chr) == -1:

print(chr+" is not in "+word)

If the character is present, this is executed

else:

print(chr+" is in "+word)

The main begins here

This gets the character

chr = input("Character: ")

This gets the word

word = input("Word: ")

This calls the method and prints the required output

print(charInWord(chr,word))

User Raviraj
by
4.5k points