def prime():

guess = input('gimme a number: ')

```
for x in range (2, int(guess)):
if guess % x == 0:
print ('this is not prime')
else:
print ('this is prime')
```

prime()

may i know whats wrong with my code

def prime():

guess = input('gimme a number: ')

```
for x in range (2, int(guess)):
if guess % x == 0:
print ('this is not prime')
else:
print ('this is prime')
```

prime()

may i know whats wrong with my code

Two problems:

It raises `TypeError: not all arguments converted during string formatting`

because the interpreter thinks that this line:

```
if guess % x == 0:
```

… is trying to format the string **guess** somehow, due to the presence of the % symbol.

Solution: make **guess** an int *before* the for loop. One way to do so:

`guess = int(input("number, please: "))`

Problem 2:

say you want to input 4. Here’s what you see on the screen:

```
gimmie a number: 4
this is not prime
this is prime
```

Solution: I’ll let you work on it a bit.

```
import sys
def prime():
guess = int(input('give me a number: '))
if guess == 2:
print('this is prime')
for x in range (2, guess-1):
if guess % x != 0:
print ('this is prime')
continue
elif guess % x == 0 :
print ('this is not prime')
sys.exit()
sys.exit()
prime()
```

i get few lines of ‘this is prime’ , instead of getting one only. may i know what the problem is

No need for sys.exit(). Just use break.

Start with this

```
for x in range (2, guess-1):
if guess % x == 0 :
print ('this is not prime')
break
```

Run it; what happens? Now, think carefully, what what you want the alternative behavior to look like?

In other words, say the entire for loop (not each individual step) completes without triggering the of condition. What should happen?

```
def prime():
guess = int(input('give me a number: '))
if guess == 2:
print('this is prime')
for x in range (2, int((guess**0.5)+1) ):
if guess % x == 0:
print('this is not prime')
break
else:
print('this is prime')
prime()
```

i did this but it doenst work for 45…

OK, square root of guess is good.

But, I said, *only do this much*:

```
for x in range (2, int((guess**0.5)+1) ):
if guess % x == 0:
print('this is not prime')
break
```

… and that *does* work for 45.

Now, what you have is this:

```
for x in range (2, int((guess**0.5)+1) ):
if guess % x == 0:
print('this is not prime')
break
else:
print('this is prime')
```

… so, when x == 2, the first time around the loop, the if condition is not true, so it goes to else. Is this what you want? Do you want it to go to else every time that if is not true?

Would it not be better to wait until all of the **for** loop has been completed, then *if and only if no divisor has been encountered*, print “this is prime”?

One way:

- Declare a variable
**prime**, set it to**True** - If the if condition becomes
**True**(i.e.,**guess**is*not prime*), set`prime = False`

before**break** - No
**elif**or**else**needed: outside of the**for**loop, just test whether**prime**is True or False. If True, print the appropriate message; if False, do nothing.