OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Execute and match once each if statements into a for loop using python

  • Thread starter Thread starter Franky77
  • Start date Start date
F

Franky77

Guest
I have some if statements into a for loop; I need to find and eventually match once each if statement.

example: From my database I have some horses with their running data; I set some variables to find specific data, like this:

Code:
horses = "Showtime Mahomes (GB)"
courses = "Doncaster"
distances = "5f"
pos1 = "1"
pos2 = "2"

So, I need to find if these data matches with my database records, but doesn't matter if my data matches three times, I count just once.

I am trying to do it with a loop for with some if statements. This is my code:

Code:
from typing import Generator
from requests_html import HTMLSession
import pandas as pd
import numpy as np
from itertools import islice
import mysql.connector
from sqlalchemy import create_engine

horses = "Showtime Mahomes (GB)"
courses = "Doncaster"
distances = "5f"
pos1 = "1"
pos2 = "2"

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="horseracinguk"
)


engine = create_engine('mysql://root@localhost/horseracinguk?')


mycursor = mydb.cursor(dictionary=True)

mycursor.execute("SELECT * FROM mytable")

myresult = mycursor.fetchall()

for row in myresult:
   if horses == row["horse"]:
     if ((courses == row["course"]) and (pos1 == row["pos"])):   
        pointscourse1 =+ 2

        horse = row["horse"]
        course = row["course"]
        distance = row["dist"]
        position = row["pos"]
        print(horse, course, distance, position, pointscourse1)
        break
     
     if ((courses == row["course"]) and (pos2 == row["pos"])):   
        pointscourse1 =+ 2

        horse = row["horse"]
        course = row["course"]
        distance = row["dist"]
        position = row["pos"]
        print(horse, course, distance, position, pointscourse1)
        break
     
     if ((distances == row["dist"]) and (pos1 == row["pos"])):   
        pointsdist1 =+ 3
        horse = row["horse"]
        course = row["course"]
        distance = row["dist"]
        position = row["pos"]
        print(horse, course, distance, position, pointsdist1)
        break

So, if my horse "Showtime Mahomes (GB)" has ran and arrived 1st, I got 3 points, but just once (doesn't matter if He did it three times, I count just one) And so on...

I hope it's clear, sorry but I am a newbie of python

Thanks for your help

EDIT: This code doesn't work because it use just first IF STATEMTENT and don't use the others
<p>I have some if statements into a for loop; I need to find and eventually match once each if statement.</p>
<p>example:
From my database I have some horses with their running data;
I set some variables to find specific data, like this:</p>
<pre><code>horses = "Showtime Mahomes (GB)"
courses = "Doncaster"
distances = "5f"
pos1 = "1"
pos2 = "2"
</code></pre>
<p>So, I need to find if these data matches with my database records, but doesn't matter if my data matches three times, I count just once.</p>
<p>I am trying to do it with a loop for with some if statements.
This is my code:</p>
<pre><code>from typing import Generator
from requests_html import HTMLSession
import pandas as pd
import numpy as np
from itertools import islice
import mysql.connector
from sqlalchemy import create_engine

horses = "Showtime Mahomes (GB)"
courses = "Doncaster"
distances = "5f"
pos1 = "1"
pos2 = "2"

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="horseracinguk"
)


engine = create_engine('mysql://root@localhost/horseracinguk?')


mycursor = mydb.cursor(dictionary=True)

mycursor.execute("SELECT * FROM mytable")

myresult = mycursor.fetchall()

for row in myresult:
if horses == row["horse"]:
if ((courses == row["course"]) and (pos1 == row["pos"])):
pointscourse1 =+ 2

horse = row["horse"]
course = row["course"]
distance = row["dist"]
position = row["pos"]
print(horse, course, distance, position, pointscourse1)
break

if ((courses == row["course"]) and (pos2 == row["pos"])):
pointscourse1 =+ 2

horse = row["horse"]
course = row["course"]
distance = row["dist"]
position = row["pos"]
print(horse, course, distance, position, pointscourse1)
break

if ((distances == row["dist"]) and (pos1 == row["pos"])):
pointsdist1 =+ 3
horse = row["horse"]
course = row["course"]
distance = row["dist"]
position = row["pos"]
print(horse, course, distance, position, pointsdist1)
break

</code></pre>
<p>So, if my horse "Showtime Mahomes (GB)" has ran and arrived 1st, I got 3 points, but just once (doesn't matter if He did it three times, I count just one)
And so on...</p>
<p>I hope it's clear, sorry but I am a newbie of python</p>
<p>Thanks for your help</p>
<p><strong>EDIT:</strong> This code doesn't work because it use just first IF STATEMTENT and don't use the others</p>
 

Latest posts

Online statistics

Members online
1
Guests online
6
Total visitors
7
Top