
See the following example: num 123 string data print the type print Type of num is :, type (num)) convert using int () num int (num) print the type again print Now, type of.

Try to convert string to int python how to#
This is also my first real attempt at a recursive function and there probably is a better way to structure this, so any suggestions on how to change things up to be more effective is most welcome. If you want to convert a number that is represented in the string to int, you have to use int () function to do so. I must not be grasping a larger principle here, so any explanations would be helpful. It seems like to me that the issue comes when I try and call that happy_or_not function inside of the happy_number_check function, which is reading my txt file (containing a list of numbers to test). I tested the happy_or_not function by itself, and it does print out the value that I expect it to. It is my understanding that I have to convert the str type into an int type before doing any math on it, yet it looks like that is where it is failing. ValueError: invalid literal for int() with base 10: ''īased on what I gathered from other similar questions, the issue may be that I am not able to convert a str into an int when I hit the line i = int(i). Here is the full Traceback: Traceback (most recent call last):įile "/happy_number.py", line 55, in happy_number_checkįile "/happy_number.py", line 33, in happy_or_not If the number is a happy number, print out 1. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1, or it loops endlessly in a cycle which does not include 1.Those numbers for which this process ends in 1 are happy, while those that do not end in 1 are unhappy.ħ is a happy number (7->49->97->130->10->1)Ģ2 is not a happy number (22->8->64->52->29->85->89->145->42->20->4->16->37->58->89.


I have been stuck on a Codeval problem (Happy Numbers) for quite some time and I am not sure what is going wrong. I am new to Python, so I apologize if this is a simple fix.
