Python and Mysql tuple search

Asked

Viewed 114 times

0

Hello, Good Night!!! I am new to Python programming, and recently I received a job in college to implement a program similar to Akinator, using the desired language, because it is easier, I decided to use Python. The idea is to ask the user questions, and return only one value based on the answers obtained. I did this in a mysql test database, in a static way, with "backers" questions in the code, and a select at the end using all parameters. However, this method will be too much work to create the bank and for the user to answer, as it would have to answer 50 questions for the result to be unique. So I thought I’d ask a table of questions and a table of results. My idea is to ask random questions and use an id in the question to perform the select. Example: - Is the car brand Fiat? According to the answer, I would like to select, storing the results that satisfy the condition, and after that perform a new question applying new select only at these values already filtered. With this I believe that the number of questions would be smaller, and the same could be done randomly (one of the teacher’s requirements). However, I have no idea how to apply this in practice, that is, how to store the results somewhere, and carry out the new select only in these stored values. This loop would be held until the result shown is = 1. Could you help me? Here’s what I’ve done so far.

import pymysql

con = pymysql.connect('localhost', 'root', '', 'python')

with con:
    cur = con.cursor()
    cur.execute("SELECT VERSION()")
    version = cur.fetchone()
    print("Database version: {}".format(version[0]))

print("Ola eu sou o adivinhator!!!")
print("Pense em alguma coisa e eu vou adivinhar!!!")
print("Responda as perguntas utilizando 's' para SIM e 'n' para NÃO seguido da tecla ENTER!!!")
comecar = input("Podemos começar?\n")
if comecar == "S":
    print("La vai a primeira pergunta")
else:
    print("esperando voce")
vmarca = input("O carro e da marca FIAT?\n")
vportas = input("O carro tem 2 portas?\n")
vpotencia = input("O carro é 1.0?\n")
vtipo = input("O carro é utilitário?\n")
vtipo2 = input("O carro é hatch?\n")
qry_p = ("SELECT nome  FROM resultados where fiat = '"+vmarca+"'AND 2portas='"+vportas+"'AND pot='"+vpotencia+"'AND utilitario='"+vtipo+"'AND hatch='"+vtipo2+"'")
cur.execute(qry_p)
result = cur.fetchone()
print("O carro que pensou é", result)

1 answer

0

You can store the line id’s after each answer and use this as a condition in the next question with WHERE id IN (id1, id2, id3,...,idn). Keep asking until the id’s list has only 1 element.

  • In your example, would I store the results ID, based on select is that? My main question in this case, would be how to store this tuple/ID list and then perform the next select only on these stored values, could give me an example?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.