1
x = int(input())  
for i in range(2,x):    
     if(x % i ==0):      
        print("not Prime")  
        break  
else :  
    print("Prime")  

In this example, I am asking the user to input a value for x. So let's say the user inputs 6 since it is not a prime number, it will say "not Prime." However, I want to ask the user to input another value to check.

x = int(input())  

for i in range(2,x):  
    if(x % i ==0):      
        print("not Prime")  
        break  
else :  
    print("Prime")  

x = int(input()) 

Doing this does not work. so what can I do to ask the user to input another value? without having to run it again?

0

2 Answers 2

2

In your second code snippet you are not doing anything after the second input. Perhaps you are forgetting to call the forloop again? This works for me to do what you described. Does the following work for you? Of course using a function is more elegant for this.

x = int(input())  

for i in range(2,x):  
    if(x % i ==0):      
        print("not Prime")  
        break  
else :  
    print("Prime")  

x = int(input())

for i in range(2,x):
    if(x % i ==0):
        print("not Prime")
        break
else :  
    print("Prime")

Here is how it runs in my pycharm

enter image description here

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

10 Comments

the second snippet is simply just a failed attempt. I was saying tht it does not work. First attempt works, but the problem is if i want to user to input again he has to run the program again. I would like him to input right after the ouput without having to run the program again
hang on ill put a screenshot of pycharm, that might explain my problem to you
Someone helped me solve it actually. I needed to put it in a function. to be able to ask for input a second time. Thank you so much for trying to help me. Sorry I was bad at explaining it. I'm still learning python, so i am really bad at explaining it properly. Sorry!
No worries! Best of luck in your future coding endeavors!
Ah I see what you did there. You made another for loop. That's pretty clever
|
2

This is exactly what functions are for. Wrap your logic in a function and call it wherever you need:

def check_prime(x):
    for i in range(2,x):  
        if(x % i ==0):      
            print("not Prime")  
            break  
    else :  
        print("Prime")  

x = int(input())
check_prime(x)  # first number
x = int(input())
check_prime(x)  # another number

Note if you want some n numbers to be entered, using an infinite loop to read input() can avoid repetition.

1 Comment

Oh my god! that is exactly what I needed. Thank you so much. I haven't learned about functions yet so I was struggling. Thank you.

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.