![]() |
applying 2 conditions to a loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: applying 2 conditions to a loop (/thread-6117.html) |
applying 2 conditions to a loop - Pedroski55 - Nov-07-2017 SOLVED: I was trying if maxScore == 'None': continue I needed to write if str(maxScore) == 'None': continue The situation is this: 1. I am using openpyxl to insert scores to my target excel table, then copy over all the old data from the source file. 2. I reach the stage where I have copied all the old scores into the new table successfully. 3. Now I want to calculate the % scores and put them in the column left of the column 'score' . I look along row 3 of my table and look for the string 'score'. 4. Above 'score', in row 1 is a number. This is the maximum score for this test, maxScore. 5. Use maxScore and the students score in each column to calculate the % score for each score in that column. Easy. This works fine when maxScore is not 'None' The trouble comes when the content of the maxScore cell is 'None'. Sometimes, for that sheet, for that class, there is nothing that week. I need a condition to ignore that column and look for the next column with 'score' if the cell in row 1 above 'score' has content 'None'. So I need 2 conditions: 1. row 3 cell contains 'score'. 2. row 1 above 'score' is not 'None'. I've tried various things without success so far. Grateful for any tips! for sheet in targetFileSheetNames: targetFileActiveSheet = targetFile.get_sheet_by_name(sheet) maxRow = targetFileActiveSheet.max_row maxCol = targetFileActiveSheet.max_column print('sheet is ' + sheet) for colNum in range(8, maxCol + 1): if targetFileActiveSheet.cell(row=3, column=colNum).value == 'score': maxScore = targetFileActiveSheet.cell(row=1, column=colNum).value print('The maximum score is ' + str(maxScore)) for rowNum in range(4, maxRow + 1): value = targetFileActiveSheet.cell(row=rowNum, column=colNum).value print('value is ' + str(value)) percentScore = int(value / maxScore) * 100 print('The percent score is ' + str(percentScore)) targetFileActiveSheet.cell(row=rowNum, column=colNum - 1, value=percentScore) RE: applying 2 conditions to a loop - nilamo - Nov-08-2017 Quote: SOLVED: I was trying Don't use str() like this, just refer to None itself, instead of a string which happens to contain "None". >>> x = None >>> x == "None" False >>> x is "None" False >>> x == None True >>> x is None True |