0

Q6: Write code using the turtle module to draw a rectangle of consecutive ints from the int in the upper left to the int in the lower right. If the int is one of a pair of twin primes, the twin primes should be red. If the int is a prime (but not a twin prime), the int should be blue. Otherwise, the int is black.

This is my code so far:

import turtle

UL = 4686 
numRows = 33
numCols = 51
max_number = UL + numRows*numCols - 1
numbers = [ list(range(UL + (row *numCols), UL + (row*numCols) + numCols)) for row in 
range(numRows)]


turtle.tracer (0,0)
t = turtle.Turtle ()

## Question 2:
 def count_twinprime (UL, max_number):
count = 0
for a in range (UL, max_number):
    b = a + 2
    if (is_prime(a) and is_prime(b)):
        count += 1

print ("Number of twin primes are: ", count)
print ('Q2') 
twinPrimes = count_twinprime(UL, max_number)

Question 6:


def drawRectangle(tObj, size, text, color):
   tObj.penup()
   tObj.forward(size//2)
   tObj.color(color)
   tObj.write(number, align='center', font=("Arial", 8, "normal"))
   tObj.forward(-size//2)
   tObj.pendown()
   tObj.color('black')
   for side in range(4):
       tObj.forward(size)
       tObj.left(90)
 print("Q6:\tSee rectangle of ints showing primes & twin primes\n")

twinPrime = count_twinprime (UL, max_number)
size = 30
number = UL

for row in range(numRows):
   for col in range(numCols):
      if number in twinPrimes:
         color = 'red'
      elif is_prime(number):
            color = 'blue'
      else:
         color = 'black'
      drawRectangle(t, size, number, color)
      t.forward(size)
      number += 1
   t.penup()
   t.setx(0)
   t.right(90)
   t.forward(size)
   t.left(90)

t.hideturtle()
turtle.update()

I keep getting this how do I solve it?

Traceback (most recent call last):
File "/Users/bk/Documents/csc_proj.py", line 109, in 
<module>
    if number in twinPrimes:
TypeError: argument of type 'NoneType' is not iterable
Dhana D.
  • 1,670
  • 3
  • 9
  • 33
Betty
  • 1
  • 2
  • Does this answer your question? [Python Function Returning None](https://stackoverflow.com/questions/21471876/python-function-returning-none) – Gino Mempin Aug 14 '21 at 03:24

1 Answers1

0

The count_twinprime function isn't returning anything, therefore it's implicitely returning None. To fix this, just add a return statement to it:

def count_twinprime(UL, max_number):
    count = 0
    for a in range(UL, max_number):
        b = a + 2
        if (is_prime(a) and is_prime(b)):
            count += 1
    return count

or the one-liner

def count_twinprime(UL, max_number):
    return sum(1 for a in range(UL, max_number) if is_prime(a) and is_prime(a + 2))

Note that you can't do

twinPrimes = count_twinprime(UL, max_number)

and later do

for number in twinPrimes:

The former assigns an integer to twinPrimes (the number of twin primes between UL and max_number), and the latter tries to iterate over that integer, and you can't do that. There's a couple of alternatives you might consider:

  • If you want to iterate from 0 until the number of twin primes between UL and max_number, replace the loop with
for number in range(twinPrimes):
  • If you want to iterate over all twin primes between UL and max_number, you need to create a new function that does that:
def twin_primes(UL, max_number):
    return (a for a in range(UL, max_number) if is_prime(a) and is_prime(a + 2))

Now you can do

twinPrimes = twin_primes(UL, max_number)
for number in twinPrimes:
    ...
enzo
  • 9,861
  • 3
  • 15
  • 38
  • i did that and now it says.Traceback (most recent call last): File "/Users/bk/Documents/csc_project.py", line 122, in if number in twinPrime: TypeError: argument of type 'int' is not iterable – Betty Aug 14 '21 at 03:06
  • I've added an edit to my answer, see if this fixes this problem! However, if another error appears, you may consider [opening a new question](https://meta.stackexchange.com/q/43478) related to that error. – enzo Aug 14 '21 at 12:25
  • okay so I wrote the def twin_primes (UL,max_number) on question 6 and did the twinPrimes = twin_primes(UL,max_number). When I run the code it didn't show me any error but when making the table part, it didn't mark the twin prime as red. – Betty Aug 14 '21 at 22:49