24  Working with Repetition Structures

24.1 Task : Creating a selective reversed list

24.2 Task : Managing a selective List

24.3 Task : Managing exceptions

  • File repetitions.py
from istec.p4.exercises import decisions

bigcar = "Big Car"
mediumcar= "Medium Car"
smallcar = "Small Car" 

def myrepetition(list1,list2):  
    
    rlist1 = [list1[index] for index in decisions.odds(len(list1))]
    rlist2 = [list2[index] for index in decisions.odds(len(list2))]
    rlist1.reverse()
    rlist2.reverse()

    return rlist1,rlist2

def myrepetition2(list,tuple):
    dictionary = {}
    for x in decisions.odds(len(list)):
        if tuple[x] <= 600:
            continue
        if tuple[x] > 650:
            dictionary[list[x]] = bigcar
        elif tuple[x] > 620:
             dictionary[list[x]] = mediumcar
        else:
            dictionary[list[x]] = smallcar   

    return dictionary
    

def myrepetition3(list1,list2,list3):
    dictionary = {}
    for x in range(len(list1)):
        if list2[x] <= 600:
            continue
        try:
            size = list2[x]/list3[x]
        except:
            size = 0
        finally:
            if size > 20:
                dictionary[list1[x]] = bigcar
            elif size > 15:
                dictionary[list1[x]] = mediumcar
            elif size > 0:
                dictionary[list1[x]] = smallcar
            else:
                dictionary[list1[x]] = "Unknown" 
                
    return dictionary