Click here to Skip to main content
15,795,712 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
import random
n = random.randrange(1,10)
guess = int(input("Enter any number: "))
while n!= guess:
    if guess < n:
        print("Too low")
        guess = int(input("Enter number again: "))
    elif guess > n:
        print("Too high!")
        guess = int(input("Enter number again: "))
print("you guessed it right!!")

What I have tried:

This is a code that I have written and worked on but I feel like part of it could be coded differently to improve its quality. I just don't know how to go about it. Any modifications that I could possibly make?
Updated 25-Nov-22 6:07am

1 solution

You need to define/decide what you mean by "quality", it's a very subjective term.

For me code has "quality" if it works and is easy to read (and therefore easier to maintain).

The technique you want to research is called "refactoring"

Here are a couple of blogs that give some tips on how to go about that
Quick Python Refactoring Tips | Python Engineer[^]
Quick Python Refactoring Tips (Part 2) | Python Engineer[^]
There are many other articles available.

Just be careful to not introduce bugs or change code behaviours while you refactor, and learn when enough is enough - i.e. when to stop
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900