0

Hello everyone I have my code almost done but I'm trying to add in some sort of check to avoid errors. But I'm not really understanding what statement would be better to use to test the code. I know there are a few options of either using a loop, if-statement, or try. But here is the code in regards to doing captcha. I need it to run the first set of code which if the captcha doesn't pop up I continue on. But if the captcha does pop up solve it then continue on.

Some times captcha doesnt appear and if I run the whole set of code I get an error because we are expecting captcha to pop up.

Or if the captcha does appear to solve it which would.

I would really appreciate any help please as I'm not sure the right statement to use.

2
  • How do you define sometimes captcha doesnt appear (1%, 20%, 50%, ...)? Commented Feb 24, 2022 at 20:25
  • the very low percentage I have nothing exact. But maybe 5% of the time captcha doesn't appear? Commented Feb 24, 2022 at 20:29

2 Answers 2

1

try should be used when something would return an error and would otherwise cause your program to stop/crash. An example of this would be:

try:
    import pandas
except:
    print("Unable to import pandas.  Module not installed")

In this example your program will attempt to import the pandas module. If this fails it will then print out a line of text and continue running.

if statements are used to decided when to do something or not based on the returned logic. The key difference is that logic IS returned and not an error.

if x > 10:
   print("This is a large number")
else: 
   print("This is a small number")

With this example, if 'x' did not exist it would produce an error, no more code will be executed, and the program will crash. The main difference between IF and TRY is whether logic is returned as true/false or is something just plains fails.

With your specific example it is important to know if the captcha appearing or not will break your code. Does the logic boil down to captcha = false or does captcha not exist at all and logic fails entirely?

Sign up to request clarification or add additional context in comments.

Comments

0

Q: How do you define sometimes captcha doesn't appear (1%, 20%, 50%, ...)?

A: Maybe 5% of the time captcha doesn't appear.

In this case, I prefer to use Exception handling: do stuff and if something goes wrong, fix it

try:
  # > 95% of the time the code below works when the captcha appears
except SomeException:
  # < 5% of the time the code is called when the captcha doesn't appear

IMHO, you have not really 2 different codes: you have one and a fallback solution, it's really different than:

if x % 2:
    ...
else:
    ...

7 Comments

So the majority of the time I need to solve captcha so something like try: bottom set of code, except someexception: the top portion of the code? and then else: continue with middle code?
Yes, I guess that's what I would do. I hope you get what I mean :)
Do I need to define some exception for it to run? or will the statement run if an error occurs during the try: statement?
Sorry. I think I misunderstood the problem! Wait a minute.
How do you detect the captcha?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.