查找错误 python

Finding error python

如果有人可以帮助我找到我的缺陷,我希望脚本做的是每分钟向行附加一个不同的随机数。我得到的结果只是给了我相同的行数。

from openpyxl import Workbook
from openpyxl import load_workbook
import random
import schedule     
import time
import datetime     

def create_excel(info, info2):
    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([info, info2])
        n += 1
        wb.save('sample.xlsx')


def main():
    print('Starting program now....')
    int = random.randint(55, 99)
    int2 = random.randint(1, 50)
    excel(int, int2)
    print('Finished program.')


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

问题在于 ws.append([info, info2]) 行,它每次都附加相同的两个值。

要在每次 while 循环出现时附加一对新的整数,您可以实现一个新函数来生成一对新的随机整数。

这可行:

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