str' object does not support item assignment
In Python, strings are immutable, so you can't change their characters in-place.
You can, however, do the following:
for c in s1:
s2 += c
The reasons this works is that it's a shortcut for:
for c in s1:
s2 = s2 + c
The above creates a new string with each iteration, and stores the reference to that new string in s2
.
Getting 'str' object does not support item assignment Error from Python3
read()
returns a string, which is immutable (and thus, can't be sorted, as you've seen). If you want to get a list of lines in the file and sort them, you could use array = f.readlines()
.
TypeError: 'str' object doesn't support item deletion
You may have instantiated pizza_topping
as a list
, but you immediately start using it as a str
. It seems like you are trying to slam too many ideas together, and not considering them. You don't need toppings and a pizza. You just need a pizza and store the toppings in it.
Most of what you are trying to do can be done on one line. You don't need to delete anything, just keep overwriting the variable. Also, your loop should be tied to the 'quit' condition. I changed that condition, because typing 'quit' to quit is cumbersome. Just type nothing and press enter to quit.
s_msg = 'Enter your pizza topping or press return to quit: '
e_msg = 'Thank you for your order!\nYour toppings are:\t{}'
pizza = []
while (topping := input(s_msg)) != '':
pizza.append(topping)
print(e_msg.format(', '.join(pizza)))
There are more things to consider, though. What if the user quits, but wants to start over? What if the topping is not a valid topping? What if your user can't spell? How does your system know that pepperoni and peperony are the same thing? These are your real challenges.
str' object does not support item assignment while using char array
I corrected it by converting the key string to list and then operated on the list and at the end converted the list back to string.
import numpy as np
key=input("Enter the secret key")
key+='abcdefghiklmnopqrstuvwxyz '
size=len(key)
l_key=list(key)
for i in range(size):
if l_key[i]=='j':
l_key[i]='i'
for j in range(i+1,size):
if l_key[j]==l_key[i]:
for k in range(j,size-1):
l_key[k]=l_key[k+1]
size-=1
else: j+=1
final_key=''.join(l_key)
play = np.zeros((5, 5), 'U1')
play.flat[:32] = list(final_key)
final_key
It works now because lists are mutable.
TypeError: 'str' object does not support item assignment when trying to run thru variables to write to them
I think you meant the line
"answear" [answeariteration] = str(answear)
to be
globals()["answear"+str(answeariteration)] = str(answear)
But this is not a good way to do it. Instead of manipulation of variable names, you can use a dict. Maybe something like this:
answer = {}
answer_iteration = 0
while answer_iteration < int(numberofanswers):
thread = reddbot.submission(url=str(submissionurl))
answer[answer_iteration] = str(thread.comments[answer_iteration])
answer_iteration += 1
And you can use a for
loop instead of while
.
answer = {}
for answer_iteration in range(numberofanswers):
thread = reddbot.submission(url=str(submissionurl))
answer[answer_iteration] = str(thread.comments[answer_iteration])
And you probably don't need to do the thread every loop, although I'm guessing about some things at this point.
answer = {}
thread = reddbot.submission(url=str(submissionurl))
for answer_iteration, comment in enumerate(thread.comments):
answer[answer_iteration] = str(comment)
And now this is simple enough to be a comprehension
thread = reddbot.submission(url=str(submissionurl))
answer = {i: str(comment) for i, comment in enumerate(thread.comments)}
Which could maybe be simplified to just
thread = reddbot.submission(url=str(submissionurl))
answer = dict(enumerate(thread.comments))
if the comments were already strings. Not sure.
And that could maybe be simplified to
thread = reddbot.submission(url=str(submissionurl))
answer = list(thread.comments)
or even
answer = list(reddbot.submission(url=str(submissionurl)).comments)
Since we were using numerical keys, starting from 0.
In these cases, instead of using answer0
, answer1
, answer2
, etc, you can use answer[0]
, answer[1]
, answer[2]
, etc.
Related Topics
How to Validate a Url with a Regular Expression in Python
How to Automatically Install Required Packages from a Python Script as Necessary
Python: Get Output of the Shell Command 'History'
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
Python Ctypes - Loading Dll Throws Oserror: [Winerror 193] %1 Is Not a Valid Win32 Application
Matplotlib: Format Axis Offset-Values to Whole Numbers or Specific Number
Reading an Excel File in Python Using Pandas
Why Does Using 'Arg=None' Fix Python's Mutable Default Argument Issue
Compare Two Columns Using Pandas
Too Many Values to Unpack', Iterating Over a Dict. Key=>String, Value=>List
Programming on Samsung Chromebook
How I Open Remote Server Folder Using Python
How to Write to a CSV Line by Line
Pygame Already Installed; However, Python Terminal Says "No Module Named 'Pygame' " (Ubuntu 20.04.1)
In Practice, What Are the Main Uses for the "Yield From" Syntax in Python 3.3