[Tutor] Not returning out the series
Hi as a challenge I have got to sum a series i / (i+1). My code isn't summing right. Any ideas why? def main(): print("{0:15s}{1:20s}".format("i","m(i)")) for i in range(1,20): print("{0:<15d}{1:<20.4f}".format(i,m(i))) def m(i): total = 0 for i in range(1,i+1,1): total+=(i/(i+1)) return total ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Re: While Loop Question
Thank you all I finally understood that the while code traps j and runs independently of the for loop until while is false and a new i is picked. On Thu, May 11, 2017 at 10:19 AM Abdur-Rahmaan Janhangeer < arj.pyt...@gmail.com> wrote: > sorry j is set to zero. grave typo > > Abdur-Rahmaan Janhangeer, > Mauritius > https://abdurrahmaanjanhangeer.wordpress.com > > On 11 May 2017 12:29 pm, "Abdur-Rahmaan Janhangeer" <arj.pyt...@gmail.com> > wrote: > > > > > > > -- Forwarded message -- > > From: "Abdur-Rahmaan Janhangeer" <arj.pyt...@gmail.com> > > Date: 11 May 2017 12:26 pm > > Subject: Re: [Tutor] While Loop Question > > To: "Rafael Skovron" <rskov...@gmail.com> > > Cc: > > > > i modified your code to make it look like that : > > > > for i in range(1, 5): > > > > j=0 > > print("outer, i=",i) > > while j < i: > > print("inner, j=",j) > > > > j += 1 > > > > so it shows that each time the outer loop is reexecuted, i is set to > zero. > > > > putting j at the begining completely, it is not reset to zero > > > > Abdur-Rahmaan Janhangeer, > > Mauritius > > https://abdurrahmaanjanhangeer.wordpress.com > > > > > > > > > ___ > 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
[Tutor] Simple while loop question
I dont understand why j can have any value other than zero in this: for i in range(1, 5): j = 0 while j < i: print(j, end = " ") j += 1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Is this a scope issue?
This project compares two text files with parcel numbers. I think I'm messing up the function call. I'm getting this error: Traceback (most recent call last): File "amador.py", line 48, in remaining_parcels(auctionlist,removedlist) NameError: name 'auctionlist' is not defined import re fname = 'saclisting.txt' removed = 'removed.txt' def get_parcel_number(fname): #this retrieves parcel numbers from the saclisting file and stores to auctionlist pattern = r'^(\d{3})-(\d{3})-(\d{3})-(\d{3})' auctionlist = [] with open(fname,'r') as file: text = file.readlines() for index,line in enumerate(text): if re.search(pattern,line): #add line to auctionlist if the pattern is found auctionlist.append(line) return auctionlist def get_removed_number(removed): #this grabs the removed parcel numbers and stores to a list pattern = r'^(\d{3})-(\d{3})-(\d{3})-(\d{3})' removedlist = [] with open(removed,'r') as file: text = file.readlines() for index,line in enumerate(text): if re.search(pattern,line): # add line to removedlist removedlist.append(line) return removedlist def remaining_parcels(auctionlist,removedlist): #compares parcels in auctionlist to removedlist and returns parcels that have not bee removed remainingparcels =[] for parcel in auctionlist: if parcel not in removedlist: remainingparcels.append(parcel) return remainingparcels get_parcel_number(fname) get_removed_number(removed) remaining_parcels(auctionlist,removedlist) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor