即使 while 循环条件应将其关闭,仍继续运行循环?
userinput = str(input(''))
def input_read() :
newstring1 = []
EB_to_01 = []
placeholder = 0
y = 0
while y <= len(userinput) :
for i in userinput[y:] :
if i != 'E' and i != 'B' :
newstring1.append(i)
y += 1
else : break
placeholder += 1
for i in userinput[y:]:
if i == 'E' :
newstring1.append(placeholder)
EB_to_01.append(i)
y += 1
elif i == 'B' :
newstring1.append(placeholder)
EB_to_01.append(i)
y += 1
else: break
print(newstring1 ,'\n',EB_to_01)
return EB_to_01
input_read()
我尝试删除 for 循环上的切片,但它不会无休止地重复。我需要切片,因为简而言之,我试图将所有 E 和 B 添加到一个单独的列表中,并按照它们在字符串中的顺序用数字替换输入中的所有 E 和 B。
while循环的条件应该是< and not <= because the loop starts at y = 0 and ends at one less than the length so never reaches the stop condition.
当您有无限循环时,一个方便的工具是在关键点(如索引)处放入打印语句以查看发生了什么。