0

actually i'm new to learning python and mysql database i have table named TABEL_NILAI in database like this:

id auto_increment primary key
NILAI_TRUST FLOAT
NAMA_ALAT VARCHAR(10)
--------------------------
|ID|NILAI_TRUST|NAMA_ALAT|
--------------------------
|1 | 0.12 | ALAT1 |
--------------------------
|2 | 0.34 | ALAT2 |
--------------------------
|3 | 0.54 | ALAT3 |
--------------------------

So i want to Retrieve value NILAI_TRUST from TABEL_NILAI and i want to do some math. here's code i was made:

kursor = db.cursor()
kursor.execute("SELECT NILAI_TRUST FROM TABEL_NILAI")
hasil = kursor.fetchall()
def hitung_standardeviasi():
 #input data
 TrustValue= x().split(',')
 for i in range(len(TrustValue)):
 TrustValue[i]= int(TrustValue[i])
 jumlah=0
 #Average
 for i in range(len(TrustValue))
 jumlah += TrustValue[i]
 ratarata = jumlah/len(TrustValue)
 total=0
 #sigma
 for i in range(len(TrustValue))
 hitung = (TrustValue[i]-ratarata)**2
 total += hitung
 sigma = total/(len(TrustValue)-1)
 #standardeviasi
 standardeviasi = math.sqrt(sigma)
 print(standardeviasi)
for x in hasil:
 hitung_standardeviasi()

And giving me error like:

Traceback (most recent call last):
 File "/home/lihat_tabel.py", line 60, in <module>
 hitung_standardeviasi()
 File "/home/lihat_tabel.py", line 24, in hitung_standardiviasi
 Trustvalue = x().split(',')
TypeError: 'tuple' object is not callable

so how to retrieve value into python variable in right ways. Please help:(

Syscall
19.8k10 gold badges44 silver badges60 bronze badges
asked Dec 13, 2019 at 13:02

1 Answer 1

1

The query

kursor.execute("SELECT NILAI_TRUST FROM TABEL_NILAI")

will return a iterable of tuples, for example like this:

[(0.12,), (0.34,), (0.54,)]

So this line

TrustValue= x().split(',')

has two problems:

  • x is a tuple, so it can't be called like x()
  • x is not a string, so it doesn't have a split method

It looks like you want to make TrustValue a list of the int values of each result in the query. In that case this for loop is unnecessary, because it will only it will make the code operate on a single result at a time:

# You don't need this
for x in hasil:
 hitung_standardeviasi()

The hitung_standardeviasi function could begin like this:

def hitung_standardeviasi():
 #input data
 results = list(hasil)
 TrustValue= []
 for i in range(len(results)):
 TrustValue[i]= int(results[i][0])
 jumlah=0 

We can make this code a more readable by looping over the results directly instead of using the for i in range(len(collection)): idiom, which is often unnecessary in Python:

def hitung_standardeviasi():
 #input data
 TrustValue= []
 for (result,) in hasil:
 TrustValue.append(int(result))
 jumlah=0

Using a list comprehension is even more Pythonic:

def hitung_standardeviasi():
 #input data
 TrustValue = [int(result) for (result,) in hasil]
 jumlah=0 

You might want to look at the sum built-in function and the statistics module too.

answered Dec 14, 2019 at 12:00
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer @snakecharmerb. but finally i used statistics modul for ending this problem.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.