TypeError: 'tuple' object does not support item assignment

前端 未结 4 1625
名媛妹妹
名媛妹妹 2021-01-24 06:20

I\'m trying to write a short program which allows the user to input a list of numbers into an input() function, and then using the add_25 function add 25 to each item in a list.

4条回答
  •  走了就别回头了
    2021-01-24 07:03

    The function input reads a string and evaluates it as a Python expression. Thus, the comma-separated list becomes a tuple of values, these are passed to add_25(), and this function tries to assign to mylist[i] something.

    And tuples are immutable, they do not support item assignment (on purpose).

    You could use actual_list = list(input(...)) to convert the tuple to a list (which supports item assignment).

    But every time someone uses input(), one has to warn him: input() is a security risk. It evaluates the input of the user and thus might execute arbitrary things the user typed. This means that your program will perform what the user asks it to, with your permissions. This is normally not what is considered a good design.

    If you always will be the only user or if you trust all users of your program completely, then so be it.

提交回复
热议问题