0
# PURPOSE: to see if a user entered number (n) is prime or not prime
#
# INPUT(S): a number greater than 2. (n>2)
#
# OUTPUT(S): prime or not prime depending on what (n) is.
#
# EXAMPLES:input:  ;output:
#          input:  ;output:
################################################################################
import math

def prime(n):
    
    for i in range(2, int(n**0.5)+ 1):
        if n % i == 0:
            return False
    return True

def main():
    n = input('Enter a number: ')
    
    p = prime(n)

    while p is True:
        break
        print "The number you entered is prime."

    while p is False:
            break
            print "The number you entered is not prime." 

main()

i think there is something wrong with my print statements but i've tried tweaking them several diffferent ways and an indentation error pops up. the only way my code hasn't received errros is how it is but it doesn't print whether or not the (n) is prime. any suggestions?

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by vegaseat
0

Modify your program to use if/else statements ...

# PURPOSE: to see if a user entered number (n) is prime or not prime
#
# INPUT(S): a number greater than 2. (n>2)
#
# OUTPUT(S): prime or not prime depending on what (n) is.
#
# EXAMPLES:input:  ;output:
#          input:  ;output:
################################################################################
#import math

def prime(n):
    
    for i in range(2, int(n**0.5)+ 1):
        if n % i == 0:
            return False
    return True

def main():
    n = input('Enter a number: ')
    
    p = prime(n)
    
    if p == True:
        print "The number you entered is prime."
    else:
        print "The number you entered is not prime." 

    
main()
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.