调试(使用ipython):

In [2]: s = 'asdfasdf asdf asdf asd sdfa'

In [4]: def count_words(sentence):

...: count = 0

...: last_position = 0

...:

...: while sentence.find(" ", last_position) != -1:

...: count += 1

...: print(f'count: {count}, position: {last_position}')

...: last_position = sentence.find(" ", last_position)

...: print(f'new position: {last_position}')

...: if count > 4:

...: break

...: return count

...:

In [5]: count_words(s)

count: 1, position: 0

new position: 8

count: 2, position: 8

new position: 8

count: 3, position: 8

new position: 8

count: 4, position: 8

new position: 8

count: 5, position: 8

new position: 8

Out[5]: 5

看看原因:

In [6]: s.find?

Docstring:

S.find(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found,

such that sub is contained within S[start:end]. Optional

arguments start and end are interpreted as in slice notation.

Return -1 on failure.

Type: builtin_function_or_method

定影:

In [7]: def count_words(sentence):

...: count = 0

...: last_position = 0

...:

...: while sentence.find(" ", last_position) != -1:

...: count += 1

...: last_position = sentence.find(" ", last_position+1)

...: return count

...:

In [8]: count_words(s)

Out[8]: 5

无限循环python代码_python - 代码遇到意外的无限循环? - SO中文参考 - www.soinside.com...相关推荐

  1. python调用cplex_python - 如何使用docplex(python)在优化问题中建模约束? - SO中文参考 - www.soinside.com...

    我需要解决类似于背包问题的优化问题.我在这篇文章中详细介绍了优化问题:knapsack optimization with dynamic variables我实际上需要使用python而不是OPL, ...

  2. python用函数绘制椭圆_python - 如何使用python从3个点找到椭圆的方程 - SO中文参考 - www.soinside.com...

    听起来很有趣!如果您的3个点击点位于同一象限中,则由这些点定义的三角形的一个角度必须是钝角.调用B和其他两个顶点A和C. x-y定向椭圆的一般方程中有4个参数.将A,B和C的x和y坐标代入椭圆方程将给 ...

  3. python安装依赖失败_python - pip安装jq依赖关系失败 - SO中文参考 - www.soinside.com

    这是我在stackoverflow上的第一个问题.我成功地安装了其他需要的包,如箭头,但我无法安装.jq. https:/pypi.orgprojectjq. 我尝试安装 jq 在Win10上使用此命 ...

  4. python画曲线的趋势线_python - 在时间序列图中添加趋势线 - SO中文参考 - www.soinside.com...

    如果通过"趋势线"表示文字行,那么您可能希望对数据进行线性回归.在sklearn的provides this functionality python. 从上面的超链接示例: im ...

  5. python数组转换为列表_python - 将一系列数组转换为单个列表 - SO中文参考 - www.soinside.com...

    我需要将数组中具有数组的序列转换为列表.这是系列:0 0 [[136.26198653744652]] 1 [[595.1701354429704]] 2 [[106.31607570796812]] ...

  6. python 字典操作 内存占用,python - 如何强行释放字典使用的内存? - SO中文参考 - www.soinside.com...

    我正在研究一个Python脚本,该脚本查询几个不同的数据库以整理数据并将所述数据持久保存到另一个数据库.该脚本从大约15个不同数据库中的数百万条记录中收集数据.为了尝试加快脚本速度,我提供了一些缓存功 ...

  7. python 在末尾增加一个字符串,python - Python File.write在末尾添加额外的字符串 - SO中文参考 - www.soinside.com...

    我正在使用python(3.6)更新文本文件,并打开r +with open(f+'.play', 'r+') as f2: play = f2.read() result = manipulate( ...

  8. range函数python循环次数_Python的range函数与for循环语句

    Python的range函数与for循环语句 米粒教育 发布时间:18-11-1518:49 介绍Python for循环语句和range函数的使用,文中主要讨论for循环语句,Python的for循 ...

  9. python顺序结构代码_Python代码结构——顺序、分支、循环

    ## 顺序结构 - 按照从上到下的顺序,一条语句一条语句的执行,是最基本的结构 ## 分支结构 if condition: statement statement ... elif condition ...

最新文章

  1. CloudStack 制作window模板
  2. 每天进步一点点:(11)进程优先级学习 nice
  3. python通过cookie绕过验证码_Python Selenium Cookie 绕过验证码实现登录示例代码
  4. winrar皮肤的更换
  5. Android串口编程--开关灯Demo(附源码)
  6. bpmn2 vue 设计器_vue项目中使用bpmn-基础篇
  7. Python较为经典的53个Python库
  8. LinkedList专题1
  9. 支持 vulkan android,Vulkan 使用入门
  10. 群辉 NAS 配置 iSCSI 存储
  11. python上传文件到onedrive_python-onedrive使用教程【linux备份至onedrive】 | C/C++程序员之家...
  12. 【c】C语言编程写的一个http下载程序
  13. 计算机文件丢失系统无法启动,文件损坏或丢失windows无法启动_windows无法启动文件损坏解决方法...
  14. SYN Flood攻击原理及防御技术
  15. landlord攻略_全攻略:在卡尔加里如何当好房东-之(三)合同篇
  16. C6820摄像头的托管驱动 - Codeplex
  17. 数字孪生智慧医院:构建三维医疗管控系统
  18. 用C/CCC++实现输出双声道(立体声).wav 文件
  19. STM32+0.96OLED的多级菜单设计
  20. C语言没有string类型

热门文章

  1. 基于JAVA+Servlet+JSP+MYSQL的学生卡消费统计管理系统
  2. BZOJ.3495.[PA2010]Riddle(2-SAT 前缀优化建图)
  3. LevelDB的源码阅读(三) Get操作
  4. Caffe Batch Normalization推导
  5. Spring4 SpringMVC Hibernate4 Freemaker 集成示例
  6. 教学楼电梯调度需求分析
  7. JavaScript与JSP区别
  8. python sqlite3写入内存_Python SQLite内存缓存
  9. 03:计算书费【一维数组】
  10. Python使用openpyxl和pandas处理学生成绩Excel文件实用案例