如何在命令行中增加变量的值? [英] How do i increase the value of an variable in a command line?

查看:43
本文介绍了如何在命令行中增加变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a = 1
for i in range(5):
    browser.find_element_by_xpath("/html/body/div[6]/div/div/div[2]/div/div/div[1]/div[3]/button").click()
    sleep(1)

我想在每个循环中将 div[1] 中的 1 增加 1+,但我该怎么做?

I want to increase the 1 in div[1] by 1+ every loop, but how can i do that?

我想我需要添加一个值,执行+a+"最后一个a = a + 1"每次都增加价值,但没有奏效.

i thought i need to add a value, do "+a+" and last of all a "a = a + 1" to increase the value every time, but it didnt worked.

a = 1
for i in range(5):
    browser.find_element_by_xpath("/html/body/div[6]/div/div/div[2]/div/div/div["+a+"]/div[3]/button").click()
    a = a + 1
    sleep(1)

推荐答案

for i in range(1,6):
    browser.find_element_by_xpath("/html/body/div[6]/div/div/div[2]/div/div/div["+str(i)+"]/div[3]/button").click()
    sleep(1)

你不需要2个变量,循环中只需要一个变量i,用str()将其转换为字符串,并添加到你需要的地方,很简单.i 的值随着循环从 1 到 5 的每次迭代而增加,完全符合您的需要.

you don't need 2 variables, just one variable i in the loop, convert it to string with str() and add it to where you need it, pretty simple. the value of i increases for every iteration of the loop going from 1 to 5 doing exactly what you need.

这篇关于如何在命令行中增加变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆