【发布时间】:2015-10-22 16:16:38
【问题描述】:
读取一串 1 和 0。计算连续 1 的个数 和连续 0 的数量, 直到最后。
例如,
s = "10001110000111"
输出应该是:
1 1's
3 0's
3 1's
4 0's
3 1's
我需要帮助来解决这个问题,只使用字符串函数(没有 find 函数)和 while/for 循环。
我有这个:
myString = input("Please enter a string of 0s and 1s: ")
zeroCount = 0
oneCount = 0
index = 0
while index < (len(myString) -1):
if myString[index] == "0":
zeroCount += 1
if myString[index +1] == "1":
zeroCount = 0
elif myString[index] == "1":
oneCount += 1
if myString[index +1] == "0":
oneCount = 0
index += 1
我做错了什么?
【问题讨论】:
-
欢迎来到 Stack Overflow!您似乎在要求某人为您编写一些代码。 Stack Overflow 是一个问答网站,而不是代码编写服务。请see here学习如何写出有效的问题。
标签: python python-3.x python-3.4