-3
dataList = list(input('Enter a data value'))
for x in range(0, 11):
    dataList.append(list(input("Enter a data value")))
    rect(210, 499, 50, (dataList[1]))

Basically, I want to take input from the user, they will enter some integers. These integers will be used as height values for rectangles. Currently, python returns the error that type str can't be used as a height value. Makes sense. When I try map or list conversions, I get errors saying type conversion to a str must operate on an object, not on a list.

How do I make the list values be evaluated by Python as integers, so that they can be placed into the rect function by calling their place as above?

Zack S
  • 9
  • 3

1 Answers1

1

Just cast each input as an int while the user types it in. And you don't have to cast each input to a list when appending to your list. Simply appending the data will be what you need. The way you were trying to create your data structure, you were ultimately doing this:

[['1'], ['2'], ['3']]

Which is definitely what you do not want. What you want is:

[1, 2, 3]

Which is simply done as dataList.append(1)

Furthermore, I do not know why you are collecting data in to a list then passing that list to a method over each iteration, but for whatever reason, if that is what you are doing, the first iteration will fail, since there will not have a dataList[1]. If you are looking to pass all your data to the method then you should outdent that rect method so it isn't in your loop

If you are using Python 2 use raw_input instead of input. Below is a Python 3-friendly example:

for x in range(0, 11):
    dataList.append(int(input("Enter a data value")))
rect(210, 499, 50, (dataList))
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • `line 28, in dataList = list(int(input('Enter a data value'))) TypeError: 'int' object is not iterable` Is the error I get using this code – Zack S Mar 14 '16 at 20:23
  • @ZackShapiro You are not even using the code answer I provided. You just pasted this code: `dataList = list(int(input('Enter a data value')))`. Stop casting it to a list. Look at my answer carefully. Also, pay attention to what I wrote about your `rect` method. – idjaw Mar 14 '16 at 20:28
  • `line 31, in dataList.append(int(input("Enter a data value"))) TypeError: descriptor 'append' requires a 'list' object but received a 'int'` Now it's telling me it can't make lists of integers?? – Zack S Mar 14 '16 at 20:40
  • I have no idea what the rest of your code looks like for you to be getting that error.. – idjaw Mar 14 '16 at 20:52