How do I take user input in a list as integers in Python 3

后端 未结 1 1879
庸人自扰
庸人自扰 2021-01-29 15:33
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]))
         


        
1条回答
  •  情歌与酒
    2021-01-29 15:52

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

    0 讨论(0)
提交回复
热议问题