0
problem
how can i add the variable base to the loop in order to add 1 to it for each circle ? a=int(input("Primo numero")) #22 2*11 b=int(input("secondo numero")) divisori=[2,3,5,7,11,13,17] listaA=[] listaB=[] base=0 for a in range(0,a+1): if a%int(divisori[base])==int(0): listaA.append(divisori[base]) a=a/divisori[base] # 11 elif a%divisori[base]!=0: base+=1 print(listaA)
16 Respuestas
+ 4
It seems to work if you use numbers like 2 and 7. I don't really understand what you're trying to do though. Note that you have to enter all numbers in separate lines as soon as you run the code:
2<enter>
7
+ 4
I see. I made a couple of changes and it seems to work:
a=int(input("Primo numero")) #22 2*11
#b=int(input("secondo numero"))
divisori=[2,3,5,7,11,13,17]
listaA=[]
#listaB=[]
#base=0
for base in range(0,len(divisori)-1):
while a%divisori[base]==0:
listaA.append(divisori[base])
a=a/divisori[base] # 11
base+=1
print(listaA)
The main issue is that you need to use a while loop instead of an if clause
+ 4
You're welcome ☺️
+ 3
Francesco Zanella I've heard a lot about sites like codewars and project Euler, but I never tried them 😞
+ 2
for a in range(0,a+1):
base += 1
No?
+ 2
It's defined right before the for loop
No?
+ 2
It seems to work if you use numbers like 2 and 7. I don't really understand what you're trying to do though. Note that you have to enter all numbers in separate lines as soon as you run the code:
2<enter>
7
+ 1
Thanks for your patience
+ 1
Thanks a lot
+ 1
Congratulations for that
https://www.sololearn.com/post/77333/?ref=app
0
No because is errore base is not defined
0
Try my code, it don't run
0
And I have defined base before the for loop
0
With base+=1 on elif
0
So, I willl explain you how it works.
I want to the divisors of the given number.
But it work only if a divisor is not repeated, for example with 22
it works because it find 2 and 11 as divisors because they are not repeated.
But for example with 8 it doesnt work and it find only 2 repeated 1 time instead of 3
0
Another question can you give a site or something like that where I can find exercises beginner/intermediate for python