Python: Convert float to int in numpy array

I’m trying to convert floats to integer in python and store the integer in a string, but I’m still getting the string with float values.

Can anyone please help me to understand, how I can convert float to int and store the int value in the string?

Code having issue:


    _number_of_alphabets, _number_of_numbers, _number_of_special_characters = ranges(_size,_number_of_alphabets,_number_of_numbers,_number_of_special_characters)
    _list = alphabets(_number_of_alphabets)
    _list = append(_list,numbers(_number_of_numbers))
    _list = append(_list,special_characters(_number_of_special_characters))
    shuffle(_list)

    _password = ''

    for i in _list:
        try:
            _password+=int(i)
        except:
            _password+=i
    
    return _password

Welcome!

Thanks for formatting your code correctly (more or less); it makes it much easier to read, debug and test with. In the future, please provide the full error message and traceback that you are getting, or describe in detail, with examples, the actual output and behavior you are getting and how it differs from what you expect. Otherwise, we are left to blindly guess at what your problem is, which greatly limits the help we can provide.

In any case, your code doesn’t show what _list actually is, but I can reproduce what I’m guessing might be your problem if I assume it to be some list of floats, e.g. _list = [1.2, 2.0, 4.2]. When that happens, I get

Traceback (most recent call last):

  File "<ipython-input-12-494c2803b71f>", line 5, in <module>
    _password+=int(i)

TypeError: can only concatenate str (not "int") to str


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "<ipython-input-12-494c2803b71f>", line 7, in <module>
    _password+=i

TypeError: can only concatenate str (not "float") to str

This is a great example of why you should never use a bare except. First of all, at the very minimum, you should always use at least except Exception, as it at least won’t catch KeyboardInterrupt and other things you almost certainly don’t want to catch, and will make your program hang. Secondly, you should catch only the specific error you’re looking for. In this case, its probably a ValueError raised by int(), if the item cannot be converted to an integer.

Instead, what is happening, as the error message explains to you, is that you cannot concatnate (+) an int and a str. What is "spam" + 5? It isn’t meaningful. Same thing with a float. In order to add it to the string, as a string, you need to explicitly convert it to a string with the str function. So, just call str() on the output of int() (or the raw number), and your immediate problem should be solved.

Also, building up a string one by one in this way isn’t too terribly efficient—instead, it is best to build up a list of strings first, and then put together your full string in one go with .join().

As a sidenote, I’m not sure why you are naming all your variables as private, with a leading underscore—that’s a little odd. And you really shouldn’t call things list, since list is the name of a builtin in Python.

Best of luck!