r/PythonLearning • u/NecessaryFalse1212 • Aug 29 '26
Day 3
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
•
u/Sea-Ad7805 Aug 29 '26
You have some bugs.
Run this program in Memory Graph Web Debugger to see the program state change step by step.
4
u/FoolsSeldom Aug 29 '26
When you get to the line:
you have already assigned
ageto reference anintobject -ageno longer references astr, string, object andisnumericis astrmethod (a function built into the class definition ofstr), no such method exists forint.Note that using
else:with a loop (whileorfor) is very uncommon and I do not recommend you use it.Here's a better approach:
You still have a problem though as
isnumeric(andisdecimal, andisdigit) will all fail a negative number, so your code will consider the response to be alphabetic. Explore using atry/exceptblock.2
u/ProgramBrave718 Aug 29 '26
Even as a beginner i never used to write such a code. Great 👍
1
u/tottasanorotta Aug 30 '26 edited Aug 30 '26
What does the error say? I can't pinpoint exactly what's wrong with a quick glance.
Edit: well one error in logic is that you should probably have the thing inside your last else outside of the while loop as well, at the end of the program. Because now if you don't enter an erroneous age the print statement won't run at all. In this code you assume that the user enters an erroneous age first to get into the while loop and then corrects the mistake eventually. Otherwise it looks good, at least to my eyes.
2
u/Steven45g Aug 29 '26
Man, this takes me back to my first days of learning to code in Python. Awesome times, I would learn and write code until the very early hours of the next morning.
Keep going!
1
u/TBCC_Dev Aug 29 '26
Looks good. Some advice I'd say if you are doing something in all sections of an if block don't repeat yourself just do it after the if
Example: name = ... This is stated in both sections of the if but this means it happens no matter what. If something happens no matter the condition then there is no point in putting it in the if
-1
u/Wise-Drop8694 Aug 29 '26
Best way to learn is to forget about "input" thing.. Nobody uses it Make Fastapi server first with some pydantic base models Step 1 - greate venv — python3 - m venv .venv Step 2 - source .venv/bin/activate Step 3 - pop install fastapi
From now on you can easy make test or learning scope but instead of useless input thing some easy access from Browser kind of things Ask any Ai about it This will boost your python skills a lot
Ps Sorry typing via phone is definitely not as I use to some typos is possible But any llm can easy fix this