[Tutor] Count for loops

Rafael Knuth rafael.knuth at gmail.com
Tue Apr 4 07:04:42 EDT 2017


Sarma: thank you so much, I checked your code, it works. However, can
you enlighten me what it exactly does?
I do not understand it (yet). Thank you in advance.

file_path = "C:/Users/Rafael/PythonCode/PiDigits.txt"

with open (file_path) as a:
    b = a.read()

get_year = input("What year were you born? ")

count = 0
b = "3"+b[2:]
n = len(b)
for i in range(n-3):
    if b[i:i+4] == get_year:
        count += 1
print("Your birth date occurs %s times in PI!" % (count))


More information about the Tutor mailing list