在Python中,我正在编写一个模拟客户下订单的脚本。它将包括创建订单,向其添加行,然后签出。我目前正在做类似的事情:
api = ['login', 'createOrder', 'scanBarCode', 'addLine', 'checkout']
for apiName in apiList:
#call API
我正在将其设计为一个框架,以便在事情发生变化时添加新的API。我的设计问题是:我如何对其进行编码,以便我可以多次调用scanBarCode和addLine?类似的东西:
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
答案 0 :(得分:1)
以下内容应该让您入门:
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
答案 1 :(得分:1)
使用范围或(最好)xrange的循环:
if apiName == 'scanBarCode':
for _ in xrange(numberOfLines):
{{ do stuff }}