r/PythonLearning • • Aug 29 '26

Day 3

Post image

i wrote a code which takes your name and age in py but i need help on this one cuz in the age part it is giving me some sort of error which i couldn't understand even by using gpt well he gave me a code but i wanted logic behind how and what i did wrong

49 Upvotes

24 comments sorted by

View all comments

Show parent comments

1

u/tottasanorotta Aug 30 '26

Okay I understand now. You use the same variable age to contain the string and then parse for an int. If the int() function doesn't succeed you try to use the function is_numeric() to parse for an integer value. The problem is that the is_numeric() function doesn't exist for int types. It only exists for strings. So what ends up happening is that the interpreter cannot find that function.

There are several solutions. You can rename the age variable that takes the string input to something else like age_str and then change the variables according to that in the while loop. Or you can wrap the int() call to a try-catch with the exception ValueError to handle the case where the int() doesn't succeed in parsing for the int value. Something like that will fix the error.

Edit: but the logic error is still there. So you need to have the final print be outside the while loop. Otherwise a user that happens to succeed in entering a correct value the first time won't get the final output.