我是靠谱客的博主 阳光音响,最近开发中收集的这篇文章主要介绍TypeError: 'str' object does not support item assignment,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1 response = {}
2
3 #设置一个标志,指出调查是否进行
4 polling_active = True
5
6 while polling_active:
7
#提示输入被调查者的名字和回答
8
name = input("nwhat is your name? ")
9
response = input("Which moutain would you like to climb someday? ")
10
11
#将答案存储在字典中
12
response[name] = response
13
14
#看看是否还有人要参与调查
15
repeat = input('would you like to let another person respond?(yes/no)')
16
if repeat == 'no':
17
polling_active = False
18
19 #调查结束,显示结果
20 print('n--- Poll Results ---')
21 for name, response in response.items():
22
print(name + " would like to climb " + response + ".")
上述代码运行会出错:
1 what is your name? Jony
2 Which moutain would you like to climb someday? Huangshan
3 Traceback (most recent call last):
4
File "mountain_poll.py", line 12, in <module>
5
response[name] = response
6 TypeError: 'str' object does not support item assignment
7
8 ***Repl Closed***
后面进行修改(修改部分为红色加粗的代码),发现可以正常运行了,但是不知道是什么原因?希望有大神可以指导一下,谢谢大家!
1 response = {}
2
3 #设置一个标志,指出调查是否进行
4 polling_active = True
5
6 while polling_active:
7
#提示输入被调查者的名字和回答
8
name = input("nwhat is your name? ")
9
response[name] = input("Which moutain would you like to climb someday? ")
10
11
#将答案存储在字典中
12
# response[name] = response
13
14
#看看是否还有人要参与调查
15
repeat = input('would you like to let another person respond?(yes/no)')
16
if repeat == 'no':
17
polling_active = False
18
19 #调查结束,显示结果
20 print('n--- Poll Results ---')
21 for name, response in response.items():
22
print(name + " would like to climb " + response + ".")
修改后运行结果:
1 what is your name? Jony
2 Which moutain would you like to climb someday? Huangshan
3 Traceback (most recent call last):
4
File "mountain_poll.py", line 12, in <module>
5
response[name] = response
6 TypeError: 'str' object does not support item assignment
7
8 ***Repl Closed***
转载于:https://www.cnblogs.com/dreamer-lin/p/10805881.html
最后
以上就是阳光音响为你收集整理的TypeError: 'str' object does not support item assignment的全部内容,希望文章能够帮你解决TypeError: 'str' object does not support item assignment所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复