[Tutor] Count for loops
Rafael Knuth
rafael.knuth at gmail.com
Sat Apr 8 08:49:41 EDT 2017
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, D.V.N.Sarma డి.వి.ఎన్.శర్మ
<dvnsarma at gmail.com> wrote:
> b = "3"+b[2:] #Removing the decimal point so that there are digits only in
my_number = 3.14159
my_number = "3"+my_number[2:]
print(my_number)
This is the error code I got:
== RESTART: C:/Users/Rafael/Documents/01 - BIZ/CODING/Python Code/PPC_56.py ==
Traceback (most recent call last):
File "C:/Users/Rafael/Documents/01 - BIZ/CODING/Python
Code/PPC_56.py", line 2, in <module>
my_number = "1"+my_number[2:]
TypeError: 'float' object is not subscriptable
>>>
I am really trying to understand how to modify strings, floats and
variables from different sources.
In case of a text file, your code works, but if I apply the same to a
float assigned to a variable, it does not work.
What am I doing wrong here? Thank you so much for your patience.
> the file
> n = len(b)
> for i in range(n-3):
> if b[i:i+4] == get_year: # Taking 4 digit long chunks from successive
> positions in b and seeing whether they are equal to get_year
> count += 1 # If they are equal increase the count by 1
>
> regards,
> Sarma.
More information about the Tutor
mailing list