Python Forum

Full Version: Too many if statements working as "tags"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My code is too long as I'm repeating the same function for every if statement
I'm doing it with BeautifulSoup since it's a web scrapping project
Basically it's like this:

if onlySubscribed == "Y":

    if onlyToday == "Y":
        #Do function

    else:
        #Do function

else:

    if onyToday == "Y":
        #Do function

    else:
        #Do function
Except with a tons more "tags" or conditions
I was planning on using dictionaries but I don't know how to implement it since the results are like:

Name: name 1
Chapters: unique no. of chapters
Date : date 1

then there are like 1000 of those ^

soo.. the last will be

Name: name 1000
Chapters: unique no. of chapters
Date: date 1000
Your example is missing the most important piece of information. How does the processing change from one case to the next?
if onlySubscribed == "Y":
 
    if onlyToday == "Y":
        #Do function   <- How does this differ
 
    else:
        #Do function   <- From this?
Obviously there is a difference, otherwise there would be no need for the if statements. What is the difference?

Maybe you should provide an example from you code. A bunch of if statements strongly indicates you need to rethink your design, but it is impossible to provide any guidance without knowing what it is you are trying to do. Hopefully some real code will provide the needed insight.
(Sep-20-2021, 08:53 AM)zeek Wrote: [ -> ]I was planning on using dictionaries
I do not understand how dictionaries are relevant.
Maybe thinking of something like this:
import operator as op

operators = {'+':op.add, '-':op.sub, '*':op.mul, '/':op.truediv}
a, op, b = input('Enter equation: ').split()
print(operators[op](float(a), float(b)))
Hard to say since no information has been provided other than "web scraping". A bit vague to be helpful. I can see how you could end up with endless if statements, but there is probably a pattern to the tags that could be exploited.