Skip to content Skip to sidebar Skip to footer

How Do I Take User Input In A List As Integers In Python 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

Solution 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))

Post a Comment for "How Do I Take User Input In A List As Integers In Python 3"