Trying to add words from set to a dict

This is the place for queries that don't fit in any of the other categories.

Trying to add words from set to a dict

Postby jshaun.obryan » Thu Jun 05, 2014 12:24 am

I am trying to add words in a set created from from user input to serve as the keys of a dict. The program asks the user for a sentence and adds to the set each time a word is introduced for the first time. It then prints the keys in a vertical column with the position of the key value with with each key. If the user presses enter without typing text the program terminates. This is a homework assignment:

Code: Select all
#!/usr/local/bin/python3

wordset=set()
wordtracker=dict()

while True:
    wordinput = input("Please enter a sentence: ")
    if len(wordinput.strip())==0:
        print ("Finished")
        break
    elif len(wordinput.strip())!=0:
        for word in wordset:
            wordtracker[word] +=1
            print (wordtracker, len(word))


This is returning nothing. I just get a prompt to enter another sentence unless I hit enter without typing any text. I'm expecting a result like this:

Please enter a sentence:

This is some text

This 1
is 2
some 3
text 4

Please enter a sentence:

This is some more text

This 1
is 2
some 3
text 4
more 5

Please enter a sentence:

Finished

Thank you!
jshaun.obryan
 
Posts: 20
Joined: Fri Apr 18, 2014 2:28 am

Re: Trying to add words from set to a dict

Postby stranac » Thu Jun 05, 2014 11:52 am

Code: Select all
    elif len(wordinput.strip())!=0:
        for word in wordset:
            wordtracker[word] +=1
            print (wordtracker, len(word))

The for loop does something for every item in wordset.
wordset is empty (you set it to an empty set and never changed it), so the loop does nothing.

Also, this:
Code: Select all
if len(wordinput.strip())==0:
    # do stuff
elif len(wordinput.strip())!=0:
    # do other stuff

is better written as:
Code: Select all
if wordinput.strip():
    # do stuff
else:
    # do other stuff
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1143
Joined: Thu Feb 07, 2013 3:42 pm

Re: Trying to add words from set to a dict

Postby jshaun.obryan » Thu Jun 05, 2014 10:14 pm

Thank you. I modified the code but am now getting a KeyError:

Code: Select all

#!/usr/local/bin/python3

wordset=set()
wordtracker=dict()

while True:
    wordinput = input("Please enter a sentence: ")
    if len(wordinput.strip())==0:
        print ("Finished")
        break
    else:
        wordset = wordinput.split()
        for word in wordset:
            wordtracker[word]
            print (wordtracker)


Result:

Please enter a sentence: This is some text
Traceback (most recent call last):
File "./input_counter.py", line 14, in <module>
wordtracker[word]
KeyError: 'This'

At this point i would just like the words from the user input to become keys in the dict. I tried using the get(k, default) but that only returned empty braces for each word. Eventually I would like to get each word from the user input as the dict keys and have their position in the dict as their values.

Thanks again.
jshaun.obryan
 
Posts: 20
Joined: Fri Apr 18, 2014 2:28 am

Re: Trying to add words from set to a dict

Postby Yoriz » Thu Jun 05, 2014 11:04 pm

The way you are using
Code: Select all
wordtracker[word]

you are asking for the value from the dict wordtracker be returned where the key is word, what you want to be doing is to set a value
Code: Select all
wordtracker[word] = 'what you want it to be here'
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 827
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests

cron