Having trouble when creating new tables on SQL with python code

Viewed 34

I am new to coding after taking a MOOC on python and I started messing around for practice. I found an interesting excersice online and I took on it. The goal is to write code that calculates pi based on random x,y variables that are between 0 and 1. I completed the basic task but then I wanted to add some fluf and experiment I decided to store the data in SQL tables and make the code persistant calculating pi with more accuracy.

Now I get the following traceback when I try to run my code.

Traceback (most recent call last):
File "F:\Desktop\Programming\pi\pisql.py", line 36, in <module>
num_point_circle=int(cur.fetchone()[0])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

My code in its entirety is:

import random
import sys
import sqlite3

conn = sqlite3.connect('pi.sqlite')
cur = conn.cursor()

#creating tables for x and y and making sure x and y are unique
cur.execute('''CREATE TABLE IF NOT EXISTS X
(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, x_value REAL UNIQUE)''')

cur.execute('''CREATE TABLE IF NOT EXISTS Y
(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, y_value REAL UNIQUE)''')

cur.execute('''CREATE TABLE IF NOT EXISTS XY
(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,x_id INTEGER, y_id INTEGER, loc TEXT)''')

cur.execute('''CREATE TABLE IF NOT EXISTS Pi
(id INTEGER PRIMARY KEY, pi_value REAL, Accuracy FLOAT, parses INTEGER,
incircle INTEGER UNIQUE)''')
#accuracy here is the number of digits after 3 and .

cur.execute('''SELECT MAX(parses) OR NULL AS parses FROM Pi''')
parses=cur.fetchone()
runs=input('Enter number of points: ')
print('***Started calculating***')
n=int(runs)

#If it is the first time I run the code
if parses is None:
    num_point_circle=0
    num_point_total=0
    print('###STARTING FROM SCRATCH###')
else:
#To continue from last time
    cur.execute('''SELECT MAX(incircle) AS incircle FROM Pi''')
    num_point_circle=int(cur.fetchone()[0])
    cur.execute('''SELECT MAX(parses) AS parses FROM Pi''')
    num_point_total=int(cur.fetchone()[0])
    print('###CONTINUE FROM',num_point_total,'PARSES###')
try:
    for i in range (n):
        x=random.uniform(0,1)
        cur.execute('INSERT OR IGNORE INTO X (x_value) VALUES (?)',(x,))
        y=random.uniform(0,1)
        cur.execute('INSERT OR IGNORE INTO Y (y_value) VALUES (?)',(y,))


        distance = x**2 + y**2
        if distance <=1:
            num_point_circle+=1
            loc='Inside Circle'
            cur.execute('INSERT OR IGNORE INTO XY (x_id,y_id,loc) VALUES (?,?,?)',(x,y,loc))
        else:
            loc='Outside Circle'
            cur.execute('INSERT OR IGNORE INTO XY (x_id,y_id,loc) VALUES (?,?,?)',(x,y,loc))

        num_point_total+=1

    p=4*num_point_circle/num_point_total
    accu=(len(str(p))-2)
    print('---->pi estimate for',num_point_total,'points is',p)
    cur.execute('INSERT OR IGNORE INTO Pi (pi_value,Accuracy,parses,incircle) VALUES (?,?,?,?)',(p,accu,num_point_total,num_point_circle),)
    print('Accuracy:',accu)
    print('!!!DO NOT CANCEL. PROCEEDING TO COMMIT ON TABLE!!!')
    conn.commit()
except KeyboardInterrupt:
    p=4*num_point_circle/num_point_total
    accu=(len(str(p))-2)
    print('---->pi estimate for',num_point_total,'points is',p)
    cur.execute('INSERT OR IGNORE INTO Pi (pi_value,Accuracy,parses,incircle) VALUES (?,?,?)',(p,accu,num_point_total,num_point_circle),)
    print('Accuracy:',accu)
    print('!!!DO NOT CANCEL. PROCEEDING TO COMMIT ON TABLE!!!')
    print("***User ended calculation***")
    conn.commit()
    sys.exit()

Any ideas what I can change? Thanks a lot in advance

0 Answers
Related