PY String to Int | SoloLearn: Learn to code for FREE!


PY String to Int

While following exercise from a book, iv encountered an error in the playground. The new_age formula is suppose to be able to turn a string into an int and i keep getting an error. I understand that there are other ways to achieve the desired result but thats not what im going for here Is this a bug/oversight or is this intentional.

7/10/2020 5:03:09 AM


3 Answers

New Answer


How to fix TypeError? Instead of print("Your dog " + dog_name + " is " + new_age+ " in human years.") , change it to print("Your dog " + dog_name + " is " + str(new_age) + " in human years.") , or fancier method using formatted string literals (Python >= 3.6) print(f"Your dog {dog_name} is {new_age} in human years.")


Marie use one of them at line 9 print("Your dog " + dog_name + " is " + str(new_age) + " in human years.") print(f"Your dog { dog_name} is {new_age} in human years.") ------------------------------------------------------- str function is used to convert an integer into a string new_age is in integer, so to concatenate it with string , you have to convert it in string using str Function I hope you understood 🙂


The error is when i try to turn the string into an int. My apologies if i didnt make that clear. Edit: I made the changes and it works now :) Thank you for your help.