Print list of numbers in a range

Asked

Viewed 43 times

-5

I’m having trouble creating a program that asks the user for two values and prints all primes in that sequence (including the numbers typed)

Here comes the code:

number_range1=int(input('Digite o primeiro número do seu intervalo:'))
number_range2=int(input('Digite o segundo número do seu intervalo:'))

i=0
number=number_range1
numbers=[]

while i<=(number_range2-1):
    numbers.append(number)
    number=number+1
    i+=1

i=1
k=0
r=0
primes=[]
j=len(numbers)

while k<j:
    while i<=numbers[k]:
        d=numbers[k]%i
        if d==0:
            r+=1
        i+=1

    if r==1 or r==2:
        primes.append(numbers[k])
        
    k+=1

print(primes)
  • The way it is, your code gives syntax error on line 4 due to the ```at the end of it. Are you sure that’s the code? Indentation is true to its original code?

  • quote must have been an error in posting but the code is the same =/

  • 1

    Okay, so I recommend you try to do the table test explaining your code, because there are many snippets that do not make much sense and I imagine that it was not clear even for you, so the table test will help you understand your own code.

  • 1

    And try to answer things like "why if I enter the values 10 and 20, the variable numbers from 10 to 29?"

  • thank you very much Woss

1 answer

0

I really appreciate Woss performing a table test with the variables was a learning that I will take to life ,I managed to remake the algorithm and really as you said many things didn’t make sense any my logical basis is still very weak here goes the result:

n=int(input())
c=int(input())

k=c-n+1

numbers=[]

for x in range(0,k):
    numbers.append(n)
    n+=1

primes=[]
for y in range(0,len(numbers)):
    i=1
    t=0
    while i<=numbers[y]:
        d=numbers[y]%i
        i+=1
        if d==0:
            t+=1
    
    if t==1 or t==2:
        primes.append(numbers[y])

print(primes)

        

Browser other questions tagged

You are not signed in. Login or sign up in order to post.