-5

I need this for my homework. I'm trying to create a a program where I ask the user for some words. I want those words to be on a list so then I can use .index to call some.

I tried to use .set or sorted(set()). It worked with numbers but not words. For instance:

x = list (raw_input ("insert numbers"))
set = set(x)
result = list(set)
result.sort()
print result

From this, I get some good results. If I feed it numbers, I get the output I expect:

insert numbers 817654
[' ', '1', '4', '5', '6', '7', '8']

If I enter words, it sorts the letters:

insert numbers now i will a rhyme construct ...
[' ', '.', 'a', 'c', 'e', 'h', 'i', 'l', 'm', 'n', 'o', 'r', 's', 't', 'u', 'w', 'y']

The output I want is

['a', 'construct', 'i', 'now', 'rhyme', 'will']
Prune
  • 76,765
  • 14
  • 60
  • 81
Iv Ramírez
  • 11
  • 1
  • 4
  • 4
    please post some code of what you have tried – R Nar Nov 02 '15 at 21:17
  • I have a feeling that you haven't used `.split()`. But like someone else has said, please edit your post to include a sample of the inputs you've tried, and the expected outputs for those inputs – inspectorG4dget Nov 02 '15 at 21:18
  • x = list (raw_input ("insert numbers")) set = set(x) result = list(set) result.sort() print result That worked for me... with the numbers, no matter how I entered them... My question... can I do that with words? – Iv Ramírez Nov 02 '15 at 21:20
  • as @inspectorG4dget said, use `.split()` method on your `raw_input` call instead of `list` and you will have x be a list of words from the input – R Nar Nov 02 '15 at 21:21
  • 1
    `words = sorted(set(raw_input("Enter words: ").split()))` – inspectorG4dget Nov 02 '15 at 21:23
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [MCVE](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your code and accurately describe the problem. – Prune Nov 02 '15 at 21:35

1 Answers1

0

All we need to do from your original attempt is to turn the input into a word list before we do any more processing. I see that inspectorG4dget already did the one-line solution to this -- make sure to vote up his comment. If you replace "words =" with "print", there's your whole program. Closer to your terms is this code:

x = raw_input ("insert numbers")
word_list = x.split(' ')
set = set(word_list)
result = list(set)
result.sort()
print result

@inspectorG4dget, if you care to post your solution as an explained response, I'll be happy to delete this posting in your favor: your comment came in while I was editing the question.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    @-replies don't work outside of comments or chat. In the future, don't worry about "stealing" from comments; best to just keep your answer straightforward for readers who don't care at all about the conversational bits (though in this case, it's a dupe and not really worth answering) – Air Nov 02 '15 at 23:45