I tackled a beginners' exercise: "asks the user for a number and then prints out a list of all the divisors of that number."
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 "the number you entered is a primary number". 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("Please select a number between: "))
y = num/2
if not y==0:
y=int(y-0.5)
list_range = list(range(2,y+2))
divisors_list = []
if num < 4:
print("The number you entered is a primary number")
else:
for i in list_range:
if num%i ==0:
divisors_list.append(i)
if not divisors_list:
print("The number you entered is a primary number")
else:
print(divisors_list)