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
#1
Hi all

I have been using the sys module's stdinput to take input from the user until they enter the keyword 'submit'. Once this happens the dictionary would have stored the key:value pairs for what has been inputted up to this point.
The key:value pairs should print to the user.

The issue I am having is that I can't stop the std input with the keyword 'submit', curious why this is?

import sys
dictionary_words = {}
for line in sys.stdin:
    for word in line.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)
If its of any help, when I compile this py file, I run it in a linux terminal.

Much appreciated

P.S Unfortunately do have to use stdin :)
Reply
#2
You may try to .strip() any non-printable chars from word,
before you test it. You never know.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#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
#4
break takes you out of the inner loop, but you are still stuck in the outer loop. An easy way to break out of both is make this a function and use return.
def get_word_count():
    count = {}
    for line in sys.stdin:
        for word in line.split():
            if word == 'submit':
                return count
            count[word] = count.get(word,0) + 1

for key,value in get_word_count().items():
    print(key,value)
I see this a lot and it makes me wonder where it comes from. There is no reason to for the else in this code:
        if word == 'submit':
            break
        else:  # Does nothing!
            dictionary_words[word] = dictionary_words.get(word,0) + 1
If the word is 'submit' it will break out of the loop and not update the word count. If the word is not submit it will update the word count in the dictionary. The code does the same thing with or without the else.
Reply


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,287 Nov-23-2022, 07:53 PM
Last Post: Larz60+
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,256 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Word count vanjoe198 1 1,939 Mar-16-2021, 12:27 AM
Last Post: BashBedlam
  Changing Directory based on user input paulmerton4pope 13 7,884 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,759 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  count unique letters in a word sunny_awesome 4 8,642 Jun-06-2019, 07:15 PM
Last Post: kotter
  Word based Game of 21 DatNerdKid 2 65,822 Aug-24-2018, 03:25 PM
Last Post: DuaneJack
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,065 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