查找错误python

扫码查看
本文介绍了查找错误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 the same number down the rows.

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])行上,该行每次都附加相同的两个值.

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

要在每次出现while循环时添加一对新的整数,可以实现一个新函数,该函数生成一对新的随机整数.

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.

这将起作用:

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

这篇关于查找错误python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:49
查看更多