![]() |
string error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: string error (/thread-12145.html) |
string error - satyam - Aug-11-2018 hey guys, I am new to python programming. I was practising a program that prints first half of a string. But I cant run the code. please identify the error I am making str=input("enter any string") length=(len(str)) if length%2==0: length=len(str) half=str[0:length/2] print(half) else: print("input even no. string")Above code is giving me following error in line number 5 in pycharm --Special Variable --length={int}6 --str={str}computer What am I doing wrong RE: string error - buran - Aug-11-2018 true division always returns float, so length / 2 returns float which is not allowed as index. use floor division length // 2 also don't use str as variable name . it's a type and you overshadow it, i.e. you will not be able ti use it if needed my_str = 'foo' print(isinstance(my_str, str)) str = 'bar' print(isinstance(my_str, str))
RE: string error - satyam - Aug-12-2018 thanks mate |