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
#3
(May-16-2017, 08:37 AM)franklee809 Wrote: 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

Thanks
Reply


Messages In This Thread
RE: Someone explains to me how this works please - by franklee809 - Jun-20-2017, 07:56 AM

Forum Jump:

User Panel Messages

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