-1

I am trying to translate I am Jason using this code, but when I use it, it prints out nothing, unless I do one word like hello, then it prints out guten tag, but that's it. Plus, I can't turn the input data into lowercase to be able to compare it to the dictionary. What can I do to make this code work?

name = input("Please enter your name\n")
data = [input("Please enter sentence\n")]
data = data.lower() #to make sentence lowercase to be able to compare it to the words in the dictionary
dictionary = {"hello": "hallo", "i" : "ik", "am" : "ben"}

dictionary[name] = name
for word in data:
    if word in dictionary:
        print (dictionary[word],)

This is the traceback

*Please enter your name

Jason

Please enter sentence

I am Jason*

Traceback (most recent call last):
  File "C:/Users...", line 3, in <module>
    data = data.lower()
AttributeError: 'list' object has no attribute 'lower'
  • Does your code produce an Exception? When asking about code that produces an Exception always include the complete Traceback - copy and paste it as text then format it as code. – wwii Mar 11 '19 at 00:57
  • I included the traceback in my edit – Jason Haven Mar 11 '19 at 01:00
  • `data = [d.lower() for d in data]` – Carson Mar 11 '19 at 01:02
  • Thank you, that removed the traceback, but it still doesn't output a sentence or anything at all and just ends the program when I write more than two words, even if they're included in the dictionary – Jason Haven Mar 11 '19 at 01:04
  • Python tells you what is wrong: `AttributeError: 'list' object has no attribute 'lower'` at line 3. So, `data` has no function lower() you can apply it to ! Because `data`is of type `[]`, not `str`. – LoneWanderer Mar 11 '19 at 01:25
  • I would suggest you trying to think about what you want your code to do and to understand step by step what is actually doing. For now, what your code does is: to put a entire sentence inside a list (without tokenizing it), then you tried to apply a function intended for strings in this one-sized list. And, after setting the corresponding dict correctly you iterate over your one-sized list. Therefore, you only get successful cases when your entire sentence is composed of only one word. – Aurora Wang Mar 11 '19 at 01:39

1 Answers1

0

You made a list out of the user's response

In [26]: data = [input("Please enter sentence\n")]

Please enter sentence
You are Jason

In [27]: data
Out[27]: ['You are Jason']

lists don't have a lower method.
First get the user's response and turn it into lower case.

In [28]: data = input("Please enter sentence\n")

Please enter sentence
You are Jason

In [29]: data
Out[29]: 'You are Jason'

In [30]: data = data.lower()

In [31]: data
Out[31]: 'you are jason'

Then split the string on whitespace to get a list of individual words.

In [32]: data = data.split()

In [33]: data
Out[33]: ['you', 'are', 'jason']
wwii
  • 23,232
  • 7
  • 37
  • 77
  • I tried the data.split() and it says data = data.split() AttributeError: 'list' object has no attribute 'split' – Jason Haven Mar 11 '19 at 01:09