Create a divisors list and identify primary numbers I tackled a beginners' exercise: &quot;asks the user for a number and then prints out a list of all the divisors of that number.&quot;
The workflow I established is: input an integer number, say x; add a variable which value is x/2, say y; declare a divisors list.
If x is greater than 4 iterate between 2 and y+1; if the remainder is zero append it the the divisors list.
If divisors list is empty or if the input number is smaller than 4 return: this is a primary number.
else, return divisors list.
I ended up with the following solution. It does the job, but isn't good enough, as it has the following issues:
What is the python's input number limit? Currently, I have no limit which is wrong as it beyond the computational capacities of any computer.
I suspect my else statement isn't tidy enough. It can be shorter... but how?
If the number is lower than 4, the script returns twice the message &quot;the number you entered is a primary number&quot;. I could fix it with an ad-hoc solution - but it should be solved through an algorithm not in a manual manner (...at least if I try to learn coding).
I ended up iterating in range of 2 and y+2 rather y+1 as I thought. I solved it manually but I don't understand why it isn't y+1.
num = int(input(&quot;Please select a number between: &quot;))
y = num/2
if not y==0:
    y=int(y-0.5)

list_range = list(range(2,y+2))

divisors_list = []
if num &lt; 4:
    print(&quot;The number you entered is a primary number&quot;)
else:
    for i in list_range:
        if num%i ==0:
            divisors_list.append(i)

if not divisors_list:
    print(&quot;The number you entered is a primary number&quot;)
else:
    print(divisors_list)
This program has 4 parts: (1) get raw user input; (2) validate the input and
convert it to its proper data type (int in this case), halting program if
invalid; (3) compute the divisors; and (4) report them to the user. However,
your current code has those things jumbled up. While collecting raw input you
try to convert it to int (which could fail) and then you turn to preparing
some variables needed to compute the divisors and then you do a little bit of
validation. Here's your code with virtually no changes other than reordering
the existing lines so we can see the conceptual boundaries better. It's very
easy to get confused when writing code, especially if the language is new to
you; a disciplined, orderly approach to keeping different things clearly
separated will help you reduce the confusion.
import sys

# Get user input.
num = input(&quot;Please select a number between: &quot;)

# Convert and validate. Stop if invalid.
num = int(num)
if num &lt; 4:
    print(&quot;The number you entered is a primary number&quot;)
    sys.exit()

# Compute divisors.
y = num/2
if not y==0:
    y=int(y-0.5)
list_range = list(range(2,y+2))
divisors_list = []
for i in list_range:
    if num%i ==0:
        divisors_list.append(i)

# Report.
if not divisors_list:
    print(&quot;The number you entered is a primary number&quot;)
else:
    print(divisors_list)

Regarding Python's number limit, don't worry about it. The only applicable
limit for a program like this is your patience. Just decide and
validate.
Perhaps there are different ways of defining a divisor, but my understanding is
that every integer N above 1 has at least two divisors -- namely [1, N].
If you were to embrace that idea, your code would simplify because there would
be no need for special handling for prime numbers. The code below starts
looking for divisors at 1; switch it to 2 to stay with your current approach.
When computing divisors, each time you find one, you usually get a second for
free. For example, if the input is 24, when you find 4 you also know that
6 is one too. Also, there's no need to check for divisors beyond the number's
square root.
Regarding your question about the list_range needing y + 1 or y + 2, I
suspect that's because Python ranges have a non-inclusive endpoint: for
example, range(1, 4) is analogous to a list of [1, 2, 3]. The divisor_rng
in the code below also has to use a +1.
Here's an illustration of those comments.
import sys
import math

# Some constants.
MIN = 1
MAX = 1000

# Get user input.
reply = input(f'Enter an integer in the range {MIN}-{MAX}: ')

# Convert and validate.
try:
    num = int(reply)
    if num not in range(MIN, MAX + 1):
        raise ValueError()
except ValueError:
    print(f'Invalid reply: {reply}')
    sys.exit()

# Compute divisors.
divisors = []
divisor_rng = range(1, int(math.sqrt(num)) + 1)
for d in divisor_rng:
    if num % d == 0:
        # Store divisor.
        divisors.append(d)
        # Store its mate, unless d is the square root.
        d2 = num // d
        if d2 != d:
            divisors.append(num // d)
divisors.sort()

# Prime check.
is_prime = num &gt; 1 and divisors == [1, num]

# Report.
print(is_prime, divisors)