Python Forum
problem with while statement.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with while statement.
#1
Hello,
I am new to python and cant seem to find an answer to my question.Here is what i am trying to do.

I want to make a loop that for example lastst for 10 seconds. Within those 10 sec I want to find an image, if that image is not found within those 10 sec i want it to run the else statement.

Here is what i have now.
   

import pyautogui
import time

t_end = time.time() + 10
while time.time() < t_end:
    if pyautogui.locateCenterOnScreen('image.png') is not None:
        print("found")
    else:
        print("Not found within the time frame")
Reply
#2
See the following link to where this was answered before by using the parameter minSearchTime
https://python-forum.io/thread-35062-pos...#pid147856
BobSmoss likes this post
Reply
#3
It is still good to know why your while loop didn't work since this is a useful pattern.
import pyautogui
import time
 
t_end = time.time() + 10
while time.time() < t_end:
    if pyautogui.locateCenterOnScreen('image.png') is not None:
        print("found")
        break
else:
    print("Not found within the time frame")
The difference is that the "else" portion of the loop is only evaluated when the loop is complete, and only if the loop reached completion by meeting the end condition (time.time() >= t_end) and not the break statement.
BobSmoss likes this post
Reply
#4
Thank you all for the help it worked XD.
SOLVED.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem in if-else statement shantanu97 2 2,441 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  multiple condition if statement problem FelixReiter 3 2,612 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 1,778 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with If else statement Milfredo 5 2,597 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Problem with a 'break' statement. christopher3786 3 2,452 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Problem with an IF statement Ryan_Todd 13 5,025 Jan-30-2020, 08:22 PM
Last Post: snippsat
  Problem with 'and' in 'if' statement CoderMan 3 2,538 Oct-06-2019, 07:32 PM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,220 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 2,284 May-20-2019, 09:26 AM
Last Post: avorane
  if statement and in operator problem bobger 5 4,043 Nov-30-2017, 06:50 PM
Last Post: bobger

Forum Jump:

User Panel Messages

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