0

I have conditions and need to define var in them and use that out of this conditions. This is my codes:

if 1 < (process_time % 60):
    final_process_time = 'process time is: ' + str(process_time) + ' sec!'
elif 1 <= (process_time % 60) < 60:
    process_time = process_time / 60
    final_process_time = 'process time is: ' + str(process_time) + ' min!'
elif 60 <= (process_time % 60):
    process_time = process_time / 3600
    final_process_time = 'process time is: ' + str(process_time) + ' hour(s)!'
print(final_process_time)

I got this error:

local variable 'final_process_time' referenced before assignment

Hints: I tested these solutions, but none of them responded:

Way 1:

set final_process_time to `global` # Error: name 'final_process_time' is not defined

Way 2:

Define final_process_time =''  Before conditions # Result: nothing set to this var 

Attached : Some of you suggested that I put more of my code to fix the bug in this section. My entire codes:(of course, some absolutely unrelated items have not been added)

def excel_report(request):
    output = io.BytesIO()
    workbook = xlsxwriter.Workbook(output)
    ....
    TITLES_LIST = [
            ['user', 'title', 'explains', 'organization', 'post', 'time_start', 'time_end'],
            ....
    ]
    FILE_NAME = 'report-' + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") + '.xlsx'
    # writer = pd.ExcelWriter(FILE_NAME, engine='xlswriter')
    if request.method == 'POST':
        if 'job_record' in request.POST:
            time_start_process = time.time()
            resumes = JobRecord.objects.all().values(*(TITLES_LIST[0][0]))
            titles_list = TITLES_LIST[0][0]
            worksheet = workbook.add_worksheet('Job Resume')
            worksheet.write_row('A1', titles_list, title_styles)
            for i in range(1, len(resumes) + 1):
                k = i - 1
                for j in range(len(titles_list) - 2):
                    worksheet.write(i, j, resumes[k][titles_list[j]], body_styles)
                worksheet.write(i, 5, resumes[k]['time_start'], time_style)
                worksheet.write(i, 6, resumes[k]['time_end'], time_style)
            process_time = time.time() - time_start_process
            if 1 < (process_time % 60):
                final_process_time = 'process time is: ' + str(process_time) + ' sec!'
            elif 1 <= (process_time % 60) < 60:
                process_time = process_time / 60
                final_process_time = 'process time is: ' + str(process_time) + ' min!'
            elif 60 <= (process_time % 60):
                process_time = process_time / 3600
                final_process_time = 'process time is: ' + str(process_time) + ' hour(s)!'
            worksheet = workbook.add_worksheet('process sheet')

            worksheet.write('A1', 'Process Time', title_styles)
            worksheet.write('A2', final_process_time, title_styles)
            workbook.close() 
            output.seek(0)
            response = HttpResponse(output,
                                content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
            response['Content-Disposition'] = 'attachment; filename=%s' % FILE_NAME
            return response
Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
rahnama7m
  • 865
  • 10
  • 38
  • if that piece of code is inside a function, the first thing you should do is writing global . The variable, anyway, has to be defined before the function is – Gianmarco F. May 20 '19 at 15:10
  • Check the value of `process_time`, I'm afraid none of the conditions evaluate to `True`. – araraonline May 20 '19 at 15:11
  • 1
    Unrelated, but I don't think `%` does what you think it does. Any number `% 60` will only ever be between 0 and 59; this won't help you determine if the number is seconds, minutes or hours. – Daniel Roseman May 20 '19 at 15:46

1 Answers1

4

Either set final_process_time to a meaningful value before the conditions, or add an else clause where you, again, set it to a meaningful value. Don't make it global, that has nothing to do with the problem.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • I did difine `final_process_time` in Way 2 but it didn't work @Óscar-lópez – rahnama7m May 20 '19 at 15:33
  • 1
    And what's the error? and what value did you use to initialise the variable? – Óscar López May 20 '19 at 15:34
  • Nothin show for me. Everythings run but `worksheet` didn't product. Initialise value: `final_process_time=''` . – rahnama7m May 20 '19 at 15:47
  • Well, there you have it. Then don't initialise the variable with `''`, as I said in my answer, use a _meaningful_ value. If you want something else to be displayed, then don't use `''`. Besides, the variable is used to store a _number_, why are you assigning a _string_ to it?. How about using `0` as an initial value? – Óscar López May 20 '19 at 15:49
  • And make extra-sure that the other conditions are actually becoming `True` at some point, otherwise you'll always fall trough to the `else` case. – Óscar López May 20 '19 at 15:49
  • Find out! `Else` and change the condition to meaningful and logical condition! – rahnama7m May 20 '19 at 16:01