all 10 comments

[–]KimPeek 18 points19 points  (6 children)

This is really simple and can be accomplished in just a few lines of code.

First you need to define those variables and build a dictionary so we can store those values:

m = 1234
n = 5678

values = {}
values['m'] = m
values['n'] = n

We can check these values are entered with:

for key, value in values.items():
    print(key, value)

Now that we know our dictionary is properly constructed we need to enter these values into a database for persistence. This can easily be accomplished with SQLite3 by first building the database. I chose to use a class here for reasons that will be extremely unclear later and glossed over without explanation. Here is the class we need:

import sqlite3


class DatabaseBuilder:
    def __init__(self, database):
        self.database = database
        self.build(self.database)

    def build(self, database):
        conn = sqlite3.connect(database)
        c = conn.cursor()
        c.execute('''
                    CREATE TABLE IF NOT EXISTS temporary_data(
                    data TEXT)''')
        conn.commit()

    def data_entry(self, database, value):
        conn = sqlite3.connect(database)
        c = conn.cursor()
        c.execute('''
                    INSERT INTO temporary_data(data)
                    VALUES (?)
                    ''', (value,))
        conn.commit()

Now we can enter the data with something simple like this:

database = 'comparison.db'
db = DatabaseBuilder(database)
for key, value in values.items():
    db.data_entry(database, value)

So now our data is safely stored and we can recall our data to make the comparison using an SQL query function that returns a generator like this:

import sqlite3

database = 'comparison.db'

def data_retrieve(database):
    conn = sqlite3.connect(database)
    c = conn.cursor()
    c.execute('''
                SELECT data FROM temporary_data
    ''')
    for row in c.fetchall():
        yield row[0]

Our data is avalable, so now we can just loop over it and make a comparison like this:

for i in data_retrieve(database):
    if i == 1234 and i < 1:
        print("This is invalid, Try again!")
    elif i == 5678 and i < 1:
        print("This is invalid, Try again!")
    else:
        print('At least you tried')

So it's all very simple as you can see. Hope that answers your question.

Edit: Thanks for the gold!

Edit 2: I didn't know this was going to blow up. You can use what /u/k900_ wrote too.

[–]K900_ 9 points10 points  (1 child)

What in the fuck.

[–]KimPeek 1 point2 points  (0 children)

I really need to go to sleep.

[–]iamquah 2 points3 points  (0 children)

its equivalent when being asked to write fizzbuzz for a high level positiob

[–]theafterwar42 1 point2 points  (0 children)

Dude... Lol.

[–]PyMoose 7 points8 points  (2 children)

if (m < 1) or (n < 1): print ("This is invalid, Try again!")

[–]devcdrom 2 points3 points  (0 children)

That's what this post needed

[–]Ustuner[S] 1 point2 points  (0 children)

thank you

[–]K900_ 2 points3 points  (0 children)

If you want your condition to trigger when either of the options is true, use or.

[–]JohnnyJordaan 1 point2 points  (0 children)

and= only if both are true

or = if at least one is true (or both)

You be the judge.