2016-02-18 87 views
-2

嗨给定的函数我有2个参数是字符串和一个Int,但我不知道哪个先到。所以我有一个函数“pick_type()”,它试图猜测参数的顺序。所以我的问题是,“pick_type()”错误地猜测了顺序,我该如何记录它,并确保“pick_type()”不再尝试特定的顺序?反馈测试python

for iteration in range(0, 10): 

    args = [] 
    print("\n def test%d(self):" % (iteration)) 
    for input in range(num_arguments): 

     args += pick_type() 

    try: 
     result = target(*args) 
     code = test_to_string(target, args, result) 


    except TypeError as error: 
     code = test_to_string_exc(target, args, error) 


    for line in code.splitlines(): 
     print(" "+line) 

def pick_type(): 
    lista = [] 

    words = ['rhythms', 'rhythms', 'manager', 'training', 'hotel', 'destroy'] 
    word = choice(words) 
    num = random.randint(-100, 100) 

    lists = [word,num] 

    choices = choice(lists) 


    if choices == word: 
     lista.append(word) 
    else: 
     lista.append(num) 


    return lista 

回答

0

我会建议将你的方法包装在一个类中,然后你可以拥有一个不良订单的持久列表。这不是我清楚你通过“订单”的意思,但不管是什么,希望这个伪代码可以帮助:

class PickType: 
    def __init__(self): 
     self._bad_orders = [] 

    def pick_type(self): 
     ...# insert body here 
     if order in self._bad_orders: 
      ## pick another order 
     ## test the new order 
     if <order is bad>: 
      self._bad_orders.append(order) 

    ## if your test for bad orders is performed outside pick_type(), you just need a method to add to the list of bad_orders 
    def add_bad_order(self, order): 
     self._bad_orders.append(order) 
+0

我说的顺序是,如果该函数在一个字符串采用第一再一个int,一个不正确的顺序将是“挑选类型”猜测int作为第一个参数,然后字符串作为第二个参数。 –

+0

所以在这种情况下,您可以将元组'((int,str)'添加到'_bad_orders'列表中 – gariepy