-1

I'm trying to write a small program for my python course(teaching myself), kinda like a dictionary using lists. One list has a phrase/word in it and the other list has the meaning of the phrase/word. Through user input the user can type the word they are searching for and the words meaning would be shown. I'm having trouble trying to get the meaning to be shown. My code is below: "aldo" is my first input(word), "my name" is my second input(meaning)

word = []
meaning = []

user_word = input("Enter word: ")
user_meaning = input("Enter Meaning: ")
print(word)
print(meaning)

word = word + [user_word]
meaning = meaning + [user_meaning]

user_search = input("What word/phrase would you like to search: ")
search_index = word.index(user_search)
print(user_search + meaning.index(search_index))
  • 1
    What do you mean by "I'm having trouble"? Is the output not, what you expect? Error messages? – Mr. T Jun 03 '18 at 22:46
  • What's the expected output? – OneCricketeer Jun 03 '18 at 22:47
  • It say the inputed "word" is not in the list –  Jun 03 '18 at 22:50
  • 1
    If I have to guess, it rather says something like "ValueError: 3 is not in list". You should provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), which includes the full traceback of the error message. – Mr. T Jun 03 '18 at 22:54
  • Try changing last line to `print(user_search + meaning[search_index])` – niraj Jun 03 '18 at 22:55
  • " File "/home/khalid/Desktop/MiniProject2/khalid_ahmed.py", line 13, in search_index = word.index(user_search) ValueError: 'aldo' is not in list " is the exact error I get –  Jun 03 '18 at 22:56
  • @student I did, it didn't work. The above error shows. I put it above in my explanation. "aldo" is my first input, "my name" is my second input –  Jun 03 '18 at 22:58
  • It would be helpful to know what were inputs. – niraj Jun 03 '18 at 22:58
  • Well, with this error message, you search for a word that is in the `meaning`, not in the `word` list. – Mr. T Jun 03 '18 at 23:10

3 Answers3

0

.index() finds an index, not a value

Did you mean to use meaning[search_index]?

Also, to add to lists, using .append(value) is preferred over + [value]

user_word = input("Enter word: ")
user_meaning = input("Enter Meaning: ")

word.append(user_word.strip())
meaning.append(user_meaning)

user_search = input("What word/phrase would you like to search: ")
search_index = word.index(user_search.strip())
print(user_search + " " + meaning[search_index])
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • it says the input "word" is not in the list –  Jun 03 '18 at 22:51
  • Well, we cannot see your input values to tell you that's true – OneCricketeer Jun 03 '18 at 22:52
  • " File "/home/khalid/Desktop/MiniProject2/khalid_ahmed.py", line 13, in search_index = word.index(user_search) ValueError: 'aldo' is not in list " is the exact error I get –  Jun 03 '18 at 22:57
  • 1
    If this has solved your question, then use the checkmark next to the post to accept it. You might be interested in this question - https://stackoverflow.com/q/22190064/2308683 – OneCricketeer Jun 03 '18 at 23:31
0

If you are trying to create dictionary, instead of list you can use dictionary to store words and meaning and later use it for searching word (for learning more about dictionary, you can look into documentation).

You can try following:

my_dictionary = {} # dictionary to store word and meaning

user_word = input("Enter word: ").lower() # .lower() for not being case sensitive
user_meaning = input("Enter Meaning: ").lower() # .lower()

# add word and meaning to dictionary
my_dictionary[user_word] = user_meaning

# search word
user_search = input("What word/phrase would you like to search: ").lower()
print("Meaning of {0} is: {1}".format(user_search, my_dictionary[user_search]))
niraj
  • 17,498
  • 4
  • 33
  • 48
0

There are a few bugs/issues in your code.

  1. Printing empty lists, not sure if this is intended as a check for debugging but you are printing the lists before appending values to them, so they will always be empty. So I'm assuming you want to print the user's input here; if not then first append the values and then print the lists.

  2. Use list.append(item) instead of list = list + [item]

  3. meaning.index(search_index) should be meaning[search_index] as list.index(item) returns index value for the first occurrence of that item in the list and not the value itself.

  4. Lastly, not very important but more of a readability issue, make sure there is a space between the word and the meaning or some kind of deliminator.

Here's the code assuming the expected behaviour:

word = []
meaning = []

user_word = input("Enter word: ")
user_meaning = input("Enter Meaning: ")
print(user_word)
print(user_meaning)

word.append(user_word)
meaning.append(user_meaning)

print(word)
print(meaning)

user_search = input("What word/phrase would you like to search: ")
print(user_search)
search_index = word.index(user_search)
print(user_search,  meaning[search_index])
Saif Azmi
  • 63
  • 6
  • 1
    Hi Saif, Thank you for pointing out that I was printing out empty lists. And thanks for the helpful point. Always Learning! –  Jun 03 '18 at 23:20