-11
num = 3    #take input from the user
if num > 1: # if input is greater than 1 check for factors
   for i in range(2,num):
       if (num % i) == 0:
               print(num,"is not a prime number")
               break
   else:
       print(num,"is a prime number")
            #if input number is less than or equal to 1, it is not prime

else:
   print(num,"is not a prime number")

I have my code here which works out the prime number of the num section, what I need to do it ask the user for the input when I run the code.

1
  • 4
    Did you do any searching at all? Commented Dec 7, 2016 at 9:31

2 Answers 2

0

This is pretty straightforward:

num = int(input("Input a number: "))
Sign up to request clarification or add additional context in comments.

4 Comments

No need for the int() unless you want to handle errors
@Saksow, the OPs code is clearly in Python 3.x
@Saksow That depends on the Python version. Casting it to an int will make it work in Python 2 and 3.
Python 2 input() evaluates the user input as an expression, raw_input() returns a string. Python 3 input() returns a string no matter what.
0

Since input() always assumes that it's taking a string ,you have to convert it to int :

print("Enter a number:")
num = int(input())   #this here
if num > 1: # if input is greater than 1 check for factors
   for i in range(2,num):
       if (num % i) == 0:
               print(num,"is not a prime number")
               break
   else:
       print(num,"is a prime number")
            #if input number is less than or equal to 1, it is not prime

else:
   print(num,"is not a prime number")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.