Python Forum
applying 2 conditions to a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
applying 2 conditions to a loop
#1
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)
Reply
#2
Quote: SOLVED: I was trying
if maxScore == 'None':
continue

I needed to write

if str(maxScore) == 'None':
continue

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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  gspread - applying ValueRenderOption to a range of cells dwassner 0 1,691 Jan-12-2022, 03:05 PM
Last Post: dwassner
  Applying function mapypy 1 2,260 Mar-11-2021, 09:49 PM
Last Post: nilamo
  Applying Moving Averages formula to a number lynnette1983 1 2,024 Sep-29-2020, 10:21 AM
Last Post: scidam
  Hi, I need help with defining user's input and applying it to code. jlmorenoc 2 2,261 Jun-24-2020, 02:10 PM
Last Post: pyzyx3qwerty
  Help with applying this instrument monitoring code to multiple inputs. Kid_Meier 1 2,082 Mar-04-2020, 12:01 PM
Last Post: Kid_Meier
  How does while-If-elif-else-If loop conditions check run mrhopeedu 2 1,771 Oct-27-2019, 04:56 AM
Last Post: mrhopeedu
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,933 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  applying and(&) ,or(|) in conditions does not result output correctly as expected Smiling29 4 2,646 Oct-21-2019, 01:39 AM
Last Post: ichabod801
  Applying row height to all rows including and after row 7 curranjohn46 2 6,556 Oct-14-2019, 03:10 PM
Last Post: curranjohn46
  for loop with 2 conditions vaison 9 6,116 Apr-19-2018, 01:28 PM
Last Post: vaison

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020