Skip to content Skip to sidebar Skip to footer

Finding Error Python

If someone could aid me in finding my flaw, what I want the script to do is every minute a different random number is appended to the rows. The result I am getting just gives me th

Solution 1:

The issue is with the line ws.append([info, info2]) which appends the same two values each time.

To append a new pair of integers on each occurrence of your while loop, you could implement a new function that generates a new pair of random integers.

This would work:

def generate_random_number():
    int1 = random.randint(55, 99)
    int2 = random.randint(1, 50)
    return int1, int2

def create_excel():
    wb = Workbook()
    ws = wb.active
    n = 1
    ws.title = 'Test ' + str(n)
    ws['A1'] = 42
    wb.save('sample.xlsx')
    while n < 4:
        wb = load_workbook('sample.xlsx')
        ws = wb.active
        ws.append(generate_random_number())
        n += 1
        wb.save('sample.xlsx')

def main():
    print('Starting program now....')
    create_excel()
    print('Finished program.')

m = 1
schedule.every(1).minute.do(main)
while m < 4:
    schedule.run_pending()
    time.sleep(60)
    m += 1

Post a Comment for "Finding Error Python"