0

Im trying to get the user to input 8 times,and after that I'm printing out the array. But the while loop doesn't seem to be stopping for some reason ? Here's my code:

from numpy import*
t = array([int()]*200)

def premier(n):
    b = True
    for i in range(2,n//2):
        if n%i==0:
            b = False
    return(b)

def quadruplets(t):
    k=1
    while k < 8:
        
        for i in range(0,len(t),4):
            
            t[i] = int(input("Give t of I: "))
            if premier(t[i]) == False:
                t[i] = int(input("Don't make this harder than it already is: "))
            t[i+1]=t[i]+2
            t[i+2]=t[i]+6
            t[i+3]=t[i]+8
            print(t)
            k+=1
            print("your k is now: ",k)
        
quadruplets(t)
print("your final table is: ",t)

1 Answer 1

3

You are using the numpy module but you're not actually utilizing any of its features, you're simply creating an array of ints using a list comprehension. Second, your while loop is never going to end because you are incrementing the value of k inside the for loop, which means that k will always be less than 8. You should increment k after the for loop instead.

Here's how you could fix your code:

from numpy import array

def premier(n):
    b = True
    for i in range(2,n//2):
        if n%i==0:
            b = False
    return(b)

def quadruplets(t):
    k = 0
    while k < 8:
        for i in range(0,len(t),4):
            t[i] = int(input("Give t of I: "))
            if premier(t[i]) == False:
                t[i] = int(input("Don't make this harder than it already is: "))
            t[i+1]=t[i]+2
            t[i+2]=t[i]+6
            t[i+3]=t[i]+8
        k += 1
        print("your k is now: ",k)
        
t = array([int()]*200)
quadruplets(t)
print("your final table is: ",t)

However, I would recommend simplifying your code further by removing the while loop and using a for loop instead, since you already know that you want to iterate 8 times:

from numpy import array

def premier(n):
    b = True
    for i in range(2,n//2):
        if n%i==0:
            b = False
    return(b)

def quadruplets(t):
    for k in range(8):
        for i in range(0,len(t),4):
            t[i] = int(input("Give t of I: "))
            if premier(t[i]) == False:
                t[i] = int(input("Don't make this harder than it already is: "))
            t[i+1]=t[i]+2
            t[i+2]=t[i]+6
            t[i+3]=t[i]+8
        print("your k is now: ",k+1)
        
t = array([int()]*200)
quadruplets(t)
print("your final table is: ",t)

Lastly, you should consider giving your variables more descriptive names to make your code easier to read and understand.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, thank you for trying to help. But it still does not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.