Python Forum
Too many if statements working as "tags"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Too many if statements working as "tags"
#1
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
Reply
#2
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.
Reply
#3
(Sep-20-2021, 08:53 AM)zeek Wrote: I was planning on using dictionaries
I do not understand how dictionaries are relevant.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  MP3 Tags to Excel Not working giddyhead 5 4,057 Jan-27-2021, 03:23 AM
Last Post: giddyhead
  Loop through tags inside tags in Selenium/Python xpack24 1 7,099 Oct-23-2019, 10:15 AM
Last Post: Larz60+
  My if and while loop statements aren't working RedSkeleton007 7 6,051 Oct-21-2017, 07:06 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020