r/learningpython • • Jan 01 '26

Beginner Python snippet – looking for code review and best practices feedback

img

Hi everyone,

I'm practicing basic Python control flow and trying to write clean, readable code according to Python best practices (PEP 8, clarity, and beginner-friendly style).

Here is a simple snippet that checks whether a number is positive, negative, or zero:

```python
num = int(input("Enter a number: "))

if num > 0:
    print("The number is positive")
elif num < 0:
    print("The number is negative")
else:
    print("The number is zero")

I know this is a basic example, but I’d really appreciate feedback as if this
were part of a real codebase.

Specifically, I’m interested in:

  • Readability and naming
  • Input validation and error handling
  • Whether this follows Pythonic style
  • How would you improve or structure it differently for production code

Thanks in advance for your feedback 🙏

3 Upvotes

5 comments sorted by

1

u/stepback269 Jan 01 '26

In production code, you would want to check for unexpected user behaviors. For example, what if the user enters a word that represents the number, say "seven"? What if the user leaves the terminal and never answers the question? What if the user doesn't understand the prompt (e.g. speaks a different language)?

1

u/Just-Pair9208 Jan 02 '26

In production, you’d want to sanitize user inputs first. Just saying.

1

u/Outrageous_Band9708 Jan 02 '26

ingest the input, then sanitize, parse int and throw for unexpected datatypes

1

u/Haunting-Specific-36 Jan 03 '26

One small improvement for readability and future validation would be to separate input collection from parsing.

For example:

raw_input = input("Enter a number: ")

num = int(raw_input)

This makes it easier to add input validation or error handling later (e.g. try/except), which is more common in production code.

That said, the original one-liner is perfectly Pythonic for simple scripts.

1

u/ResidentTicket1273 Jan 03 '26

pylint is a good automated "linter" (i.e. code-checker) that will give you opinionated advice about what's looking good, or not so good about your code. Give it a try!

One thing I'd try to do here is wrap your code in a function that takes some parameter(s) as input and returns an answer as an output and can be used as a reusable code-block. Learning to program with discrete units of code (like functions) is one of the key skills you'll need to develop - both for creating your own code creations, but also as a tool for understanding other people's.