簡體   English   中英

使用函數計算 for 循環中的操作數。 滿足條件時,從變量的基數中減去 1:Python

[英]Count the number of operations within a for loop using a function. When the condition is met, subtract 1 from the base number of the variable: Python

我想正確地編寫這段代碼,前提是 ChakCak_After = 2 在類之外代碼的任務是,每當在 for 循環中滿足條件時,ChakCak_After 中就會缺少一個數字。 如果我不檢查,數字將按原樣優先。當然,如果滿足條件,我想用類中出現的新值更改類外的 ChakCak_After 值有人有解決方案嗎?

 # -*- coding: utf-8 -*- from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.edge.service import Service from selenium import webdriver Driver = webdriver.Chrome( service=Service(ChromeDriverManager().install())) ChakCak_After = 2 class In_Cookies: def __init__(self): Driver.get("https://outlook.live.com/") ChakCak_URL = Driver.current_url ChakCak_URL_split = ChakCak_URL.strip().split("/")[3] Gam3 = "https://outlook.live.com/" + ChakCak_URL_split url_chask = "https://outlook.live.com/owa" if Gam3 == url_chask: ChakCak_After -= 1 print("Conditions are met") elif Gam3 != url_chask: print("لم يتم استيفاء الشروط") pass In_Cookies()

我沒有看到任何 for 循環。 所以我可能回答錯誤的問題,我會嘗試回答標題中提出的問題。 也許其他人會有更優雅和精致的解決方案。

1)相當粗略的方式,我會處理函數之外的東西(雖然沒有真正的邏輯方式,不把它放入函數輸入然后輸出......)是這樣的:

Count1=0
Variable=99 #dont know

def Function(Something):
  global Count1
  global Variable
  Count1+=1
  If Count1>xxx:
    Count1=0
    Variable-=1
  #rest of the function

我希望這至少有助於出價。 而且我認為那里的一些老年人的回答會不那么野蠻。

PS:使用全局時,請務必小心“命名空間 poulution”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM