Python Forum
Someone explains to me how this works please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Someone explains to me how this works please
#1
my question here

Q4.  Ask the user for a sentence and print out a table of words in that sentence. The words should be printed alphabetically with its number of occurrences next to it.  
Note: You should avoid editing the list in a for loop traverses over. If you need to edit a list use the while loop.

#Q4
s = input("Enter a sentence: ")
previous = ""
L = s.split()
L.sort()

for w in L:
   current = w
   if current == previous:
       continue
   else:
       c = L.count(w)
       print(w,c)
   previous = current
Reply


Messages In This Thread
Someone explains to me how this works please - by franklee809 - May-16-2017, 08:37 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020