Python Forum
Divisors shared the second numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: Divisors shared the second numbers (/Thread-Divisors-shared-the-second-numbers)



Divisors shared the second numbers - mircea_dragu - Feb-07-2019

I am a beginer in Python's programming, so i began with easy programming problems.
The goal is to show the divisors shared by the two numbers entered by the user.
I cannot figure out where is my mistake when i try to store in z list the divisors shared by the two numbers.


a = int(input("\n Enter first number a = "))
b = int(input("\n Enter a second number b = "))

x = []
for i in range (1, a+1):
	if(a % i == 0):
		x.append(i)
print("\n " + str(x))

y = []
for j in range (1, b+1):
	if(b % j == 0):
		y.append(j)
print("\n " + str(y))

z = []
for m in range (len(x)):
	for n in range (len (y)):
		if(x[m] == y[n]):
			z.append(n)
print("\n " + str(z))



RE: Divisors shared the second numbers - ichabod801 - Feb-07-2019

You should iterate over the list, not the indexes of the list, as shown here. Your last loop should use the in operator:

for m in x:
    if m in y:
        z.append(n)