a timer within a timer

hello and thank you in advance,
i’m currently coding The Cookie Cutter game from 100 Days of Code by Angela Yu,
the goal is to get as many clicks as possible within the span of 5 mins , while checking every 5 secs for the money (i called it “score”) i’ve accumulated so i can purchase the highest available upgrade.

this is Angela’s solution on Github:
i found it to be long and unnecessarily complicated.

my code is a bit messy as it’s still a work in progress.

what i’ve been meaning to do with my code is to iterate through a directory that contains the #id and price of upgrade, and as soon as it recognizes a match for a possible purchase it clicks on it.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import threading
import time
from collections import defaultdict
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from threading import Thread
from time import sleep
from datetime import datetime, timedelta
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://orteil.dashnet.org/experiments/cookie/")
driver.maximize_window()
#######################ELEMENTS & CONVERTING TO LISTSDICT#######################
cookie = driver.find_element(By.CSS_SELECTOR, '#cookie')
score = driver.find_element(By.CSS_SELECTOR, '#money')
all_prices = driver.find_elements(By.CSS_SELECTOR, '#store b')
all_ids = driver.find_elements(By.CSS_SELECTOR, '#store div')
new_all_prices = []
new_all_ids = []
for new_id in all_ids:
new_all_ids.append(new_id.get_attribute("id"))
for price in all_prices:
a, b, c = price.text.partition('-')
new_price = c.replace(" ", "")
if c == "":
pass
else:
new_all_prices.append(c)
tuples = [(key, value)
for i, (key, value) in enumerate(zip(new_all_prices, new_all_ids))]
myDict = dict(tuples)
#######################FUNCTIONS#######################
print(myDict)
def five_seconds_check():
for key, value in myDict.items():
if score >= key:
my_clickable_purchase = driver.find_element(By.CSS_SELECTOR, f'{myDict[key]}')
my_clickable_purchase.click()
timeout = time.time() + 5
five_min = time.time() + 60 * 5 # 1 minutes
while True:
cookie.click()
if time.time() > timeout:
five_seconds_check()
elif time.time() > five_min:
break
</code>
<code>import threading import time from collections import defaultdict from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from threading import Thread from time import sleep from datetime import datetime, timedelta chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("detach", True) driver = webdriver.Chrome(options=chrome_options) driver.get("https://orteil.dashnet.org/experiments/cookie/") driver.maximize_window() #######################ELEMENTS & CONVERTING TO LISTSDICT####################### cookie = driver.find_element(By.CSS_SELECTOR, '#cookie') score = driver.find_element(By.CSS_SELECTOR, '#money') all_prices = driver.find_elements(By.CSS_SELECTOR, '#store b') all_ids = driver.find_elements(By.CSS_SELECTOR, '#store div') new_all_prices = [] new_all_ids = [] for new_id in all_ids: new_all_ids.append(new_id.get_attribute("id")) for price in all_prices: a, b, c = price.text.partition('-') new_price = c.replace(" ", "") if c == "": pass else: new_all_prices.append(c) tuples = [(key, value) for i, (key, value) in enumerate(zip(new_all_prices, new_all_ids))] myDict = dict(tuples) #######################FUNCTIONS####################### print(myDict) def five_seconds_check(): for key, value in myDict.items(): if score >= key: my_clickable_purchase = driver.find_element(By.CSS_SELECTOR, f'{myDict[key]}') my_clickable_purchase.click() timeout = time.time() + 5 five_min = time.time() + 60 * 5 # 1 minutes while True: cookie.click() if time.time() > timeout: five_seconds_check() elif time.time() > five_min: break </code>
import threading
import time
from collections import defaultdict

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from threading import Thread
from time import sleep
from datetime import datetime, timedelta

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://orteil.dashnet.org/experiments/cookie/")
driver.maximize_window()

#######################ELEMENTS & CONVERTING TO LISTSDICT#######################
cookie = driver.find_element(By.CSS_SELECTOR, '#cookie')
score = driver.find_element(By.CSS_SELECTOR, '#money')
all_prices = driver.find_elements(By.CSS_SELECTOR, '#store b')
all_ids = driver.find_elements(By.CSS_SELECTOR, '#store div')
new_all_prices = []
new_all_ids = []
for new_id in all_ids:
    new_all_ids.append(new_id.get_attribute("id"))
for price in all_prices:
    a, b, c = price.text.partition('-')
    new_price = c.replace(" ", "")
    if c == "":
        pass
    else:
        new_all_prices.append(c)

tuples = [(key, value)
          for i, (key, value) in enumerate(zip(new_all_prices, new_all_ids))]

myDict = dict(tuples)

#######################FUNCTIONS#######################

print(myDict)


def five_seconds_check():
    for key, value in myDict.items():
        if score >= key:
            my_clickable_purchase = driver.find_element(By.CSS_SELECTOR, f'{myDict[key]}')
            my_clickable_purchase.click()


timeout = time.time() + 5
five_min = time.time() + 60 * 5  # 1 minutes

while True:
    cookie.click()
    if time.time() > timeout:
        five_seconds_check()
    elif time.time() > five_min:
        break

the part i’m struggling with is how to write a timer within a timer – meaning, how do i get the overall while loop to run for 5 mins, and simultaneously check the moneypurchase upgrade every 5 secs.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật