Python中文
首页
教程
问答
标签
搜索
登录
注册
将字符串添加到空字符串时出错
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我的程序有一个问题,该程序应该使用一个函数来反转字符串,该函数取字符串的第一个字,而该函数打印字符串时不带第一个字。你知道吗</p> <pre><code>def first_word(string): first_space_pos = string.find(" ") word = string[0:first_space_pos] print(word) def last_words(string): first_space_pos = string.find(" ") words = string[first_space_pos+1:] print(words) def reverse(string): words = string.count(" ") +1 count = 1 string_reversed = "" while count < words: string_reversed = first_word(string) + str(" ") + string_reversed string = last_words(string) count += 1 print(string_reversed) </code></pre> <p>我得到的错误是:</p> <pre><code>TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' </code></pre> <p>这句话的意思是:</p> <pre><code>string_reversed = first_word(string) + str(" ") + string_reversed </code></pre> <p>感谢您的帮助。谢谢。你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p><code>first_word</code>不返回任何内容,因此生成<code>None</code>值,不能用作带字符串的+操作数。可能您想返回<code>word</code>。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
jupyter运行一个旧的pytorch版本
6 回答
Jupyter运行不同版本的卸载库?
7 回答
Jupyter运行指定的键盘快捷键
10 回答
Jupyter通过.local文件“逃逸”virtualenv。我该如何缓解这种情况?
5 回答
Jupyter重新加载自定义样式
2 回答
Jupyter错误:“没有名为Jupyter_core.paths的模块”
8 回答
jupyter错误:无法在随机林中将决策树视为png
9 回答
Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块
4 回答
Jupyter错误地用阿拉伯语和字母数字元素显示Python列表
9 回答
Jupyter隐藏数据帧索引,但保留原始样式
9 回答
Jupyter集线器:启动器中出现致命错误。。。系统找不到指定的文件
10 回答
Jupyther中相同值的相同哈希,但导出到Bigquery时不相同
3 回答
Jupy上Python的读/写访问问题
5 回答
jupy上没有模块cv
3 回答
Jupy上的排序错误
6 回答
Jupy中bqplot图形的紧凑布局
7 回答
Jupy中matplotlib plot的连续更新
10 回答
Jupy中Numpy函数的文档
4 回答
Jupy中Pandas的自动完成问题
8 回答
jupy中Qt后端的Matplotlib动画
6 回答