Python Forum
sys.stdin to do a word count based on user entry
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sys.stdin to do a word count based on user entry
#3
(Jul-19-2020, 09:44 AM)DPaul Wrote: You may try to .strip() any non-printable chars from word,
before you test it. You never know.

Paul


hmm :( I added .strip() in the for loop, but still no good... when I type "submit" it just keeps accepting standard input and does not break the loop..


import sys
dictionary_words = {}
for line in sys.stdin:
    for word in line.strip().split():
        if word == 'submit':
            break
        else:
            dictionary_words[word] = dictionary_words.get(word,0) + 1
for key,value in dictionary_words.items():
    print(key,value)
Am I right in doing a nested for loop, since I have to capture whats in the stdin first and then break up each line by words?
Reply


Messages In This Thread
RE: sys.stdin to do a word count based on user entry - by Kaltex - Jul-19-2020, 10:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a menu and execute a function based on user selection in python? Thedarkphoenix 1 1,354 Nov-23-2022, 07:53 PM
Last Post: Larz60+
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,315 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Word count vanjoe198 1 1,986 Mar-16-2021, 12:27 AM
Last Post: BashBedlam
  Changing Directory based on user input paulmerton4pope 13 8,151 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,846 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  count unique letters in a word sunny_awesome 4 8,773 Jun-06-2019, 07:15 PM
Last Post: kotter
  Word based Game of 21 DatNerdKid 2 81,165 Aug-24-2018, 03:25 PM
Last Post: DuaneJack
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,237 Jun-19-2018, 02:52 AM
Last Post: bhill

Forum Jump:

User Panel Messages

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