Python:循环浏览列表,但重复一些项目
在Python中,我正在编写一个脚本来模拟客户下订单。它将包括创建订单、向订单中添加行,然后签出。我目前正在做类似的事情:Python:循环浏览列表,但重复一些项目,python,scripting,Python,Scripting,在Python中,我正在编写一个脚本来模拟客户下订单。它将包括创建订单、向订单中添加行,然后签出。我目前正在做类似的事情: api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout'] for apiName in apiList: #call API api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout'] numberOfLines
api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout']
for apiName in apiList:
#call API
api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout']
numberOfLines = (random number)
for apiName in apiList:
#call API
#if API name is scanBarCode, repeat this and the next API numberOfLines times, then continue with the rest of the flow
我将其设计为一个框架,以便在情况发生变化时添加新的API。我的设计问题是:如何对其进行编码,以便能够多次调用scanBarCode和addLine N?比如:
api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout']
for apiName in apiList:
#call API
api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout']
numberOfLines = (random number)
for apiName in apiList:
#call API
#if API name is scanBarCode, repeat this and the next API numberOfLines times, then continue with the rest of the flow
以下内容应该可以帮助您开始:
import random
api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout']
numberOfLines = random.randint(1, 10) # replace 10 with your desired maximum
for apiName in api:
if apiName == 'scanBarCode':
for i in range(numberOfLines):
# call API and addLine
else:
# call API
使用量程或(优选)xrange的环路: 你所说的“扫描条形码”是什么意思?这些功能是什么?它们看起来像是字符串。
xrange()
当然,只有在2.x中才需要。在3.x中,range()
返回一个iterable。