Re: [Tutor] Count for loops

2017-04-12 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
A lot of confusion is caused by the print function converting an integer or float to a string before printing to console. thus both '1234 and '1234' are shown as 1234 on the console. Similarly '15.4' and 15.4 are displayed as 15.4. There is no way to tell which is a string, which is an int and

Re: [Tutor] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp wrote: > > I have not used the decimal module (until tonight). I just now played > around with it some, but cannot get it to do an exact conversion of > the number under discussion to a string using str(). Pass a string to the

Re: [Tutor] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 3:40 AM, boB Stepp wrote: > > I have to say I am surprised by this as well as the OP. I knew that > str() in general makes a nice printable representation The single-argument str() constructor calls the object's __str__ method (or __repr__ if

Re: [Tutor] Count for loops

2017-04-11 Thread boB Stepp
On Tue, Apr 11, 2017 at 3:43 PM, Alan Gauld via Tutor wrote: > On 11/04/17 19:44, Mats Wichmann wrote: > >> import decimal >> >> Pi_Number = >> str(decimal.Decimal(3.14159265358979323846264338327950288419716939)) >> > > Unfortunately that doesn't work either: > " " +

Re: [Tutor] Count for loops

2017-04-11 Thread boB Stepp
On Tue, Apr 11, 2017 at 2:18 PM, Marc Tompkins wrote: > On Tue, Apr 11, 2017 at 9:48 AM, Rafael Knuth > wrote: > >> I tested this approach, and I noticed one weird thing: >> >> Pi_Number = str(3.14159265358979323846264338327950288419716939) >>

Re: [Tutor] Count for loops

2017-04-11 Thread Alan Gauld via Tutor
On 11/04/17 19:44, Mats Wichmann wrote: > import decimal > > Pi_Number = > str(decimal.Decimal(3.14159265358979323846264338327950288419716939)) > Unfortunately that doesn't work either: >>> " " + str(decimal.Decimal( ... 3.14159265358979323846264338327950288419716939)) '

Re: [Tutor] Count for loops

2017-04-11 Thread Mats Wichmann
On 04/11/2017 10:48 AM, Rafael Knuth wrote: > Thanks for the clarification. > I tested this approach, and I noticed one weird thing: > > Pi_Number = str(3.14159265358979323846264338327950288419716939) > Pi_Number = "3" + Pi_Number[2:] > print(Pi_Number) > > == RESTART:

Re: [Tutor] Count for loops

2017-04-11 Thread Alan Gauld via Tutor
On 11/04/17 17:48, Rafael Knuth wrote: > Pi_Number = str(3.14159265358979323846264338327950288419716939) > Pi_Number = "3" + Pi_Number[2:] > print(Pi_Number) > 3141592653589793 > > How come that not the entire string is being printed, but only the > first 16 digits? There are two problems

Re: [Tutor] Count for loops

2017-04-11 Thread Marc Tompkins
On Tue, Apr 11, 2017 at 9:48 AM, Rafael Knuth wrote: > I tested this approach, and I noticed one weird thing: > > Pi_Number = str(3.14159265358979323846264338327950288419716939) > Pi_Number = "3" + Pi_Number[2:] > print(Pi_Number) > > == RESTART:

Re: [Tutor] Count for loops

2017-04-11 Thread Rafael Knuth
>>> b = "3"+b[2:] #Removing the decimal point so that there are digits only in >> >> my_number = 3.14159 > > Here you assign a floating point number to mmy_number but > the code Sama wrote was for working with strings read > from a text file. > > You would need to convert it first: > > my_number

Re: [Tutor] Count for loops

2017-04-08 Thread Alex Kleider
On 2017-04-08 05:49, Rafael Knuth wrote: Dear Sama, thank you so much for your explanation and sorry to bother you on the same subject again. I learn the most by taking code apart line by line, putting it together, taking apart again, modifying it slightly ... which is exactly what I did with

Re: [Tutor] Count for loops

2017-04-08 Thread Alan Gauld via Tutor
On 08/04/17 13:49, Rafael Knuth wrote: >> b = "3"+b[2:] #Removing the decimal point so that there are digits only in > > my_number = 3.14159 Here you assign a floating point number to mmy_number but the code Sama wrote was for working with strings read from a text file. You would need to

Re: [Tutor] Count for loops

2017-04-08 Thread Rafael Knuth
Dear Sama, thank you so much for your explanation and sorry to bother you on the same subject again. I learn the most by taking code apart line by line, putting it together, taking apart again, modifying it slightly ... which is exactly what I did with your code. On Tue, Apr 4, 2017 at 3:20 PM,

Re: [Tutor] Count for loops

2017-04-05 Thread Neil Cerutti
On 2017-04-03, Mats Wichmann wrote: > If you're talking about 4-digit year numbers using a Western > calendar in digits of PI, the overlap effect seems unlikely to > matter - let's say the year is 1919, do we think PI contains > the sequence 191919? count would report back one

Re: [Tutor] Count for loops

2017-04-04 Thread Alan Gauld via Tutor
On 04/04/17 12:04, Rafael Knuth wrote: > Sarma: thank you so much, I checked your code, it works. However, can > you enlighten me what it exactly does? It just iterates over the PI string manually and compares the birth date with the first 4 PI string characters. It would probably be more

Re: [Tutor] Count for loops

2017-04-04 Thread Rafael Knuth
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

Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Small correction. 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

Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 04/04/17 00:37, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > I will go for this modification of the original code. > count = 0 > b= '3'+b[2:] > n = len(b) > for i in range(n-4): > if b[i:i+4] == get_year: > count += 1 While I think this works OK, I would probably suggest that this is one

Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I will go for this modification of the original code. 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-4): if b[i:i+4] == get_year: count

Re: [Tutor] Count for loops

2017-04-03 Thread Mats Wichmann
On 04/03/2017 10:16 AM, Alan Gauld via Tutor wrote: > On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: >> Sorry. That was stupid of me. The loop does nothing. > > Let me rewrite the code with some different variable names... > with open(file_path) as a: b = a.read() > > with

Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > Sorry. That was stupid of me. The loop does nothing. Let me rewrite the code with some different variable names... >>> with open(file_path) as a: >>> b = a.read() with open (file_path) as PI_text: PI_as_a_long_string =

Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 03/04/17 16:42, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > Sorry. That was stupid of me. The loop does nothing. > On the contrary, the loop does an awful lot, just not what the OP was expecting. >>> with open(file_path) as a: >>> b = a.read() >>> for year in b: >>> if get_year in b: >>>

Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
Sorry. That was stupid of me. The loop does nothing. regards, Sarma. On Mon, Apr 3, 2017 at 8:44 PM, Alan Gauld via Tutor wrote: > On 03/04/17 16:07, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > > Modifying the code as shown below may work. > > I doubt it. > > > with

Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
On 03/04/17 16:07, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote: > Modifying the code as shown below may work. I doubt it. > with open(file_path) as a: > b = a.read() > > get_year = input("What year were you born? ") > > count = 0 > for year in b: Once more I ask, what does this loop do? > if

Re: [Tutor] Count for loops

2017-04-03 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
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

Re: [Tutor] Count for loops

2017-04-03 Thread Alan Gauld via Tutor
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: >

[Tutor] Count for loops

2017-04-03 Thread Rafael Knuth
I wrote a program which checks if PI (first one million digits) contains a person's birth year. 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? ") for year in b: if get_year in b: