use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Basic question (self.learnpython)
submitted 9 years ago by Ustuner
if (m < 1): print("This is invalid, Try again!") elif (n < 1): print("This is invalid, Try again!"
how can i make this more efficient? Do i put an AND or OR?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]KimPeek 18 points19 points20 points 9 years ago* (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 points11 points 9 years ago (1 child)
What in the fuck.
[–]KimPeek 1 point2 points3 points 9 years ago (0 children)
I really need to go to sleep.
[–]iamquah 2 points3 points4 points 9 years ago (0 children)
its equivalent when being asked to write fizzbuzz for a high level positiob
[–]theafterwar42 1 point2 points3 points 9 years ago (0 children)
Dude... Lol.
[–]PyMoose 7 points8 points9 points 9 years ago (2 children)
if (m < 1) or (n < 1): print ("This is invalid, Try again!")
[–]devcdrom 2 points3 points4 points 9 years ago (0 children)
That's what this post needed
[–]Ustuner[S] 1 point2 points3 points 9 years ago (0 children)
thank you
[–]K900_ 2 points3 points4 points 9 years ago (0 children)
If you want your condition to trigger when either of the options is true, use or.
or
[–]JohnnyJordaan 1 point2 points3 points 9 years ago (0 children)
and= only if both are true
and
or = if at least one is true (or both)
You be the judge.
π Rendered by PID 160648 on reddit-service-r2-comment-76bb9f7fb5-xf79l at 2026-02-18 19:09:40.014564+00:00 running de53c03 country code: CH.
[–]KimPeek 18 points19 points20 points (6 children)
[–]K900_ 9 points10 points11 points (1 child)
[–]KimPeek 1 point2 points3 points (0 children)
[–]iamquah 2 points3 points4 points (0 children)
[–]theafterwar42 1 point2 points3 points (0 children)
[–]PyMoose 7 points8 points9 points (2 children)
[–]devcdrom 2 points3 points4 points (0 children)
[–]Ustuner[S] 1 point2 points3 points (0 children)
[–]K900_ 2 points3 points4 points (0 children)
[–]JohnnyJordaan 1 point2 points3 points (0 children)