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 your code.

On Tue, Apr 4, 2017 at 3:20 PM, D.V.N.Sarma డి.వి.ఎన్.శర్మ
<dvnsa...@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 error message is telling you exactly what is wrong:
"not subscriptable"
You've tried to use string functionality on a float.
Specifically: my_number is a float.
"""my_number[2:]""" would only make sense to the interpreter if it("my_number", not the interpretery:-) were a string (or some other sequence.)
try changing your code to:
  my_number_as_a_string = "3.14159"
my_new_number_still_a_string = "3" + my_number_as_a_string[2:] # spaces added for clarity
  print(my_new_number_still_a_string)
  my_float = float(my_new_number_still_a_string)
  print(my_float)
HTH
ps warning: not tested
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to