132k views
2 votes
Write a program that inputs a sentence from the user (assume no punctuation), then determines and displays the unique words in alphabetical order. Treat uppercase and lowercase letters the same.

User Jamesh
by
5.2k points

1 Answer

1 vote

Answer:

Following are the code to this question:

val={} #defining dictionary variable val

def unique_word(i):#defining a method unique_word

if i in val: #defining if condition to add value in dictonary

val[i] += 1#add values

else: #defining else block to update values

val.update({i: 1})#updating dictionary

s =input('Enter string value: ') #defining s variable for input string value

w=s.split()#split string value and sorte in w variable

w.sort() #sorting the value

for i in w: #defining loop for pass value in method unique_word

unique_word(i)#assign value and calling the unique_word method

for j in val:# defining for loop to print dictionary value

if val[j] == 1: #defining if block to check value is unique

print(j) #print value

Output:

Enter string value: my name is dataman

dataman

name

is

my

Step-by-step explanation:

  • In the above python code, a dictionary variable "val" is declared, which is used in the method "unique_word" that uses if block to count unique word and in the else block it update its value.
  • In the next step, s variable is declared, that the user input method to store the value and another variable "w" is defined that split and sort the string value.
  • In the last step, two for loop is declared in which the first loop passes the string value and calls the method "unique_word", and in the second loop if block is defined that check unique value and prints its value.
User JARS
by
5.0k points