Friday, 9 December 2022

Problem with the for loop in a calculation and filtering the results of the loop with a condition >=

I need help with a "for" loop for a little video game i'm making. The problem is the for of for row loop in Rows_NextFights[0:] and its contents.

Premise that the arithmetic average (or mean) is already calculated correctly for a single monster, my idea is:

  • Calculate the average Attacks for each monster (only Monster_home) from the NextFights database, based on the previous fights from the ResultsFights database. I currently calculate it for each individual monster, but I would like to calculate it for all monsters;

  • Check and compare that each Monster_home of the NextFights database has the average => 1.10 (so a condition with "if");

  • If a monster (Monster_home) does not have the mean >= 1.10 (so it has the mean < 1.10), then it won't be printed;

  • if a monster (Monster_home) has the mean >= 1.10, then the names of the monsters that satisfy the condition will be printed. Precisely I don't want to print only the name of the monster, but I would like to print the next fight (located inside the NextFights database) related to the specific Monster_home, written like this Monster_home-Monster_away (with the dash), Island;

OUTPUT I WOULD LIKE TO RECEIVE: For example, considering Godzilla, Jafar, Merlock and Crono (which are the Monster_Homes in the NextFights database) and pretending they have respectively the averages Godzilla 1.20, Jafar 0.90, Merlock 1.34, Crono 1 and I set if Mean_Attacks_Only_Monster_home >= 1.10, then I would like to print this:

Godzilla-Kong, Sun Island
Merlock-Zurg, Moon Island

ERROR: I receive no errors, the printing is correct, but badly: I receive the printing of many meaningless lists/tuples. Considering that the (wrong) output result I receive is very long, I saved the screenshot in this image uploaded to an upload image HERE

P.S: if anyone would like to suggest calculating the mean using AVG() in SQL, but i want to continue using the python code (sum and the rest, as i'm already doing)

import sqlite3

conn = sqlite3.connect('/mydatabase.db')
cursor = conn.cursor()

#Example: Godzilla-Kong, Sun Island, etc...      
All_NextFights = cursor.execute('SELECT Monster_home||"-"||Monster_away, Tourney FROM NextFights')
Rows_NextFights = [cursor.fetchall()]

Only_Monster_home = cursor.execute('SELECT Attacks, Defence FROM ResultsFights')
Rows_Only_Monster_home = cursor.fetchall()

#Mean calculation of the points of the fights
Mean_Attacks_Only_Monster_home = sum(int(row[0]) for row in Rows_Only_Monster_home) / len(Rows_Only_Monster_home)

mylist = []

for row in Rows_NextFights[0:]:
    Fights_Range = row[0]  

    if Mean_Attacks_Only_Monster_home >= 1.10:
        mylist.append(row)
        #text.insert(tk.END, (str(mylist) + '\n'))
print((str(mylist) + '\n'))

This is my database for a test:

NextFights

CREATE TABLE "NextFights" (
    "id"    INTEGER,
    "Monster_home"  INTEGER,
    "Monster_away"  INTEGER,
    "Tourney"   INTEGER,
    PRIMARY KEY("id" AUTOINCREMENT)
);

1   Godzilla    Kong    Island Sun
2   Jafar   Zod Atene
3   Talos   Spectrus    Japan
4   Merlock Zurg     Moon Island
5   Crono   Iperione    Kripton

ResultsFights

CREATE TABLE "ResultsFights" (
    "id"    INTEGER,
    "Monster_home"  INTEGER,
    "Monster_away"  INTEGER,
    "Attacks"   INTEGER,
    "Defence"   INTEGER,
    PRIMARY KEY("id" AUTOINCREMENT)
);

1   Godzilla    Jafar   2   1
2   Talos   Godzilla    3   1
3   Spectrus    Godzilla    1   2
4   Godzilla    Talos   0   0
5   Godzilla    Zod 2   3       
6   Talos   Jafar   2   1
7   Talos   BigDanger   3   1
8   Bruto   Talos   1   2
9   Zod Talos   0   0
10  Talos   Spectrus    2   3

Can you help me please? Can you show me the solution code please? Thanks in advance, to anyone who will be kind enough to help me!!!



from Problem with the for loop in a calculation and filtering the results of the loop with a condition >=

No comments:

Post a Comment