Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Divisors shared the second numbers
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):
print("\n " + str(x))

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

z = []
for m in range (len(x)):
	for n in range (len (y)):
		if(x[m] == y[n]):
print("\n " + str(z))
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:
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Nested Subdirectory within current sub on shared network drive Agregware 1 208 Jun-19-2019, 10:07 PM
Last Post: Gribouillis
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 172 May-09-2019, 12:19 PM
Last Post: Pleiades
  sysv_ipc shared memory performance comotai 1 213 Apr-08-2019, 02:27 AM
Last Post: scidam
  I can't use file __init__ to store shared variables and classes in the package AlekseyPython 2 290 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  Generate a list of numbers within a list of arbitrary numbers Takeshio 4 450 Nov-08-2018, 12:29 AM
Last Post: Takeshio
  Populating a list with divisors RedSkeleton007 1 364 Aug-21-2018, 12:52 AM
Last Post: Larz60+
  running just one process shared among uses Skaperen 3 596 Aug-07-2018, 12:12 AM
Last Post: Skaperen
  Shared reference and equality zyo 3 492 Jun-30-2018, 07:10 PM
Last Post: ljmetzger
  Shared queues l00p1n6 3 599 May-15-2018, 01:38 PM
Last Post: DeaD_EyE
  two different objects, but somehow the values are shared between them RaphaelMG 6 1,031 Apr-20-2018, 05:53 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)