gpt4 book ai didi

python - Python While 循环中的动态变量

转载 作者:行者123 更新时间:2023-12-01 01:06:20 24 4
gpt4 key购买 nike

我得到了这个 python selenium while 循环代码。

  1. 如果实际分数小于或等于 9,则执行任务 A
  2. 否则如果实际分数大于 9 则执行任务 B
  3. 执行 While 循环,直到实际点数大于 9

这是我的代码

strpoints = driver.find_element_by_class_name("fs18").text
points = slice(13, len(strpoints)-20)
actualpoints = strpoints[points]

d = 0


while (d + actualpoints <9):
# TASK A!!!
print(actualpoints + ' Points! Skipping.')
time.sleep(2)
driver.find_element_by_class_name('skip_button_single').click()

time.sleep(8)

if (d >= 10):
break
# TASK B!!!
print(actualpoints + ' Points! Go for it!')

问题:

上面的代码无法正常工作,因为变量actualpoints是动态的。

如果实际点< 9,它将执行分配的任务B但不幸的是它返回相同的变量并且永远不会改变。

任务 A,重新加载页面并显示一个新数字,该数字应存储在名为 actualpoints 的变量中。

与我的代码和变量相关的其他详细信息:

  • strpoints = 获取保存数字的字符串。该字符串的一部分是静态文本和动态(数字)。示例:您将因以下行为获得 12 分。
  • points = 对strpoints进行切片。
  • actualpoints = 对strpoints进行切片后的结果。动态值。
  • 将执行循环直到 > 10

知道代码有什么问题吗?

最佳答案

我不确定这是否能解决问题,但也许您可以添加实际点验证方法和变量来保存最后的实际点值?

这是您的代码和我添加的一些内容。如果我正确地阅读了任务 A,我将您的初始过程重新设计为 while 循环,但请随意修改它以满足您的需求。

strpoints = driver.find_element_by_class_name("fs18").text
points = slice(13, len(strpoints)-20)
actualpoints = strpoints[points]

"""
Create a temporary variable equal to the initial actualpoints value
"""
old_actualpoints = actualpoints

d = 0

def validate_actualpoints():
"""
Simple value check query. Returns actual actualpoints value.
"""
if old_actualpoints != actualpoints:
old_actualpoints = actualpoints

return actualpoints


while old_actualpoints == actualpoints:
while (d + actualpoints < 9):
# TASK A!!!
print(actualpoints + ' Points! Skipping.')
time.sleep(2)
driver.find_element_by_class_name('skip_button_single').click()

""" Move the initial process into the while loop and re-run based on TASK A """
strpoints = driver.find_element_by_class_name("fs18").text
points = slice(13, len(strpoints)-20)
actualpoints = strpoints[points]

time.sleep(8)

if (d >= 10):
break

"""
Update our temporary variable here?
(Possibly not needed.)
"""
old_actualpoints = validate_actualpoints()
break
# TASK B!!!
print(actualpoints + ' Points! Go for it!')

关于python - Python While 循环中的动态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55313537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com