Beispiel #1
0
def read_groups_screen(population):
    add_group = 'y'
    while add_group == 'y':
        cleaner.clear_screen()
        print(population)
        
        #Asking for the name of the group
        while True:
            group_name = input(f'Name of group {population.qt_groups() + 1}: ')
            if not group_name.strip():
                continue
            else:
                break
        
        #Asking for the quantity of members in the group
        while True:
            try:
                group_qt = int(input(f'Quantity of members in {group_name}: '))
            except:
                continue
            else:
                if group_qt < 1:
                    continue
                else:
                    break
        
        #Creating the group in the population
        population.add_group(group_name,group_qt)
        
        #Ask if user wants to input one more group
        add_group = str()
        while add_group not in ['y','n']:
            add_group = input('Add one more group (y/n)? ').lower()
Beispiel #2
0
def read_groups_file(population):
    error_msg = str()
    while True:
        try:
            cleaner.clear_screen()
            file_name = input(f'{error_msg}Inform valid file name: ')
            with open(file_name) as file:
                content = file.read()
            lines = content.split('\n')
        except:
            error_msg = 'File not found. '
            continue
        else:
            if lines[0] != 'SQT':
                error_msg = 'File informed has invalid data. '
            else:
                break
                
    for line in lines:
        if line == 'SQT' or not line.strip():
            pass
        else:
            try:
                broken_line = line.split(';')
                group_name = broken_line[0]
                group_qt = broken_line[1]
                population.add_group(group_name,int(group_qt))
            except:
                return False
            
    if len(population) == 0:
        return False
    else:
        return True
    '''
    pi = 3
    signal = 1
    var = 2
    for _ in range(nr_iterations):
        pi += 4 / ((var) * (var + 1) * (var + 2)) * signal
        signal *= -1
        var += 2
    return pi


if __name__ == '__main__':
    OPTION = str()

    while OPTION not in ['1', '2', 'x', 'X']:
        cleaner.clear_screen()
        print('Chooose your method:')
        print('--------------------')
        print('1. Gregory Leibniz series')
        print('2. Nilakantha series')
        print('x. Exit')
        OPTION = input('Choose your poison: ')

    if OPTION in ['x', 'X']:
        sys.exit()

    elif OPTION == '1':
        MSG = 'How many times do you want to iterate through the algorithm? '
        NR_ITER = catch_num(MSG)
        RESULT = gregory_leibniz(NR_ITER)
        print(f'Pi is ≃ {RESULT}')
        random_arm = randint(0, len(bandit) - 1)
        play_result = bandit.play_arm(random_arm)

        #Registering the result
        if play_result:
            result.win(random_arm)
        else:
            result.loss(random_arm)

    return result


if __name__ == '__main__':
    while True:
        try:
            clear_screen()
            QT_ARMS = int(input('How many arms should the bandit have? '))
        except:
            continue
        else:
            if QT_ARMS > 1:
                break
            continue

    while True:
        try:
            QT_PLAYS = int(input('How times do you want to play the bandit? '))
        except:
            continue
        else:
            if QT_PLAYS > 0: