【发布时间】:2021-03-18 22:34:58
【问题描述】:
-
所以我是编码新手,这是我一分钟前制作的一个小 Python 脚本,我不确定它是否非常有效。
-
我不确定我是否正确格式化了这个问题,这是脚本:
# Counts the number of pairs (in this case every time
# 1,6, and a period are next to each other)
counting = 0
# The variable for the array I put all the characters of
# text in.
array = []
# The variable which counts the amount of characters in
# the text document
arraycounter = 0
# For loop that goes through text document and puts it in
# my array.
for x in open("/tmp/python_001.py/Info.txt", "r").read():
array.append(x)
# While loop that goes through the array and if statement
# that checks if 1, 6, and a period are next to each other
while arraycounter + 2 < len(array):
if array[arraycounter] == '1' and array[arraycounter + 1] == '6' and
array[arraycounter + 2] == '.':
counting += 1
arraycounter += 1
# Prints the counting variable
print(counting)
【问题讨论】:
-
通过精心挑选的示例文本和您的预期输出,您的问题可能会更清楚。
标签: python arrays for-loop while-loop