Modifying the code as shown below may work. 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 for year in b: if get_year in b: count += 1 print("Your birth date occurs %s times in PI!" % (count)) regards, Sarma. On Mon, Apr 3, 2017 at 8:22 PM, Alan Gauld via Tutor <tutor@python.org> wrote: > On 03/04/17 13:22, Rafael Knuth wrote: > > > with open (file_path) as a: > > b = a.read() > > > > get_year = input("What year were you born? ") > > > > for year in b: > > Can you explain what you think this loop line is doing? > I'm pretty sure it's not doing what you expect. > > > if get_year in b: > > print("Your year of birth occurs in PI!") > > break > > else: > > print("Your year of birth does not occur in PI.") > > break > > > > As a next challenge, I wanted to check how often a person's birth year > > occurs in PI. Unfortunately, I wasn't able to figure out how to use > > the loop count properly. > > What loop count? > There is none, its a for loop, no counter needed. > (OK I just spotted your code below...) > > But there is a count() method on a string object that should help. > > > > count = 0 > > for year in b: > > if get_year in b: > > count += 1 > > else: > > print("Your birth date does not occur in PI.") > > break > > > sum_count = sum(count) > > sum() sums a sequence, but count is an integer. You have been > incrementing it as you go, the final value is already there. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor