Help with if and else coding

I have this pre-existing python code to populate a Checklist. I am modifying the Checklist and want to run a rule that if the permit number is 95 add .02 to the total postage or if the permit number is 462 add .02 to the total postage or else permit number if NA then the new postage should be the postage. I’ve tried running this with the checklist and it errors with:
File C:\Users\amikinka\Desktop\AOG\ChecklistCreator.py, line 132
else permit_num==‘462’:

SyntaxError: invalid syntax

Here’s the rule:

if permit_num==‘95’:
mrkdup_postage = (0.02 * float(total_qty))# + postage
new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

else permit_num==‘462’:
mrkdup_postage = (0.02 * float(total_qty))# + postage
new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

else permit_num==‘NA’:
mrkdup_postage = new_postage

It should be elif, instead of else, in those places that another condition needs to be checked.

Still getting the same Syntax Error: invalid syntax

I have questions in this code that I have to answer to complete the checklist and I tried entering another question the exact same way it was written for the other questions and it keeps erroring out the same way.

It’s difficult to help with a syntax error when your code is not formatted–we don’t know what you wrote.

See the pinned post in the help category for info on code formatting. Basically you want to wrap your code with ``` to preserve whitespace. Use the preview to make sure it looks the same in the post as when you were writing it.

if permit_num==‘95’:
mrkdup_postage = (0.02 * float(total_qty))# + postage
new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

elif permit_num==‘462’:
mrkdup_postage = (0.02 * float(total_qty))# + postage
new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

elif permit_num==‘NA’:
mrkdup_postage = new_postage

This is exactly how it appears in the program

if permit_num==‘95’:

new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

elif permit_num=='462':
```mrkdup_postage = (0.02 * float(total_qty))# + postage
new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

elif permit_num=='NA':
```mrkdup_postage = new_postage

maybe you mean like this?

Clearly not, because there is no indentation at all! This is one reason the code formatting is so important.

The entire block should be wrapped in a single pair of ```. You can quote this post (select my text and hit the " button) to see an example

if i_format_code_this_way:
    print("the indentation is preserved!")
else:
    print("it's very difficult to see where things went wrong")

I’m so new to this so I’m learning as I go. Sorry.

if permit_num=='95':
    mrkdup_postage = (0.02 * float(total_qty))# + postage
new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

elif permit_num=='462':
    mrkdup_postage = (0.02 * float(total_qty))# + postage
new_postage = float(postage) + mrkdup_postage
print(f"the new marked up postage is: {new_postage}")

elif permit_num=='NA':
    mrkdup_postage = new_postage

Does this work?

If that is your code, then I can explain the syntax error: you need to continue the indentation in the first block after if permit_num == '95', like so:

if permit_num=='95':
    # note that all three of these lines are indented to the same level
    mrkdup_postage = (0.02 * float(total_qty))# + postage
    new_postage = float(postage) + mrkdup_postage
    print(f"the new marked up postage is: {new_postage}")

elif permit_num=='462':
    # ... and so on

An if statement can have multiple parts: the initial if block, zero or more elif blocks, and an optional else block. It could just be a single if block with no other parts.

In your original code, the interpreter sees that the line new_postage is not indented, and so it concludes “ah, this if statement is over”. Later when it sees elif it is confused because elif can only be part of an if statement, and it’s not currently in one.

Hey James,

so that worked. Thank you. It accepted it but alas when I tried entering the question info at the last question I’m receiving a Name Error: for new_postage that I didn’t receive before.

The Checklist is designed in excel and I have to label what column and line to place the information. I added a line for new_postage and it’s not accepting it.

chklst_sheet['O27'] = new_postage #postage cost into checklist
chklst_sheet['O28'] = postage #postage cost into checklist

I’ve always had postage in the code but added the new_postage because I want the updated price to be populated.

Maybe you are receiving NameError because you set that variable (new_postage) only in some if-branch. If (heh…) that’s the case, then when your program chooses one branch then the variable may be undefined. And thus the error :frowning:

1 Like

so I deleted the (new_postage) and left in only the (postage) and it worked

but now if I switch back to a permit # (95) it is erroring out. I think this will need a one on one fix with someone. These little fixes I’m trying to make isn’t working without someone seeing those who program coding.

Does anyone know a person who does that?

Hi,

based on the thread, I would recommend putting this project aside and studying a few tutorials that teach the fundamentals of these conditional statements. A lot of these tutorials are pretty good and include a fair deal of worked out examples that you can follow along with minimal effort. Once you feel a bit more comfortable, then go back to your original project. I think that this should help you greatly as they can clear up the current misunderstandings.

Here is a potential tutorial but you can of course do your own search.

There is also the match / case statement that does essentially the same thing.

Read your original post.

You want to add to the postage for certain permit numbers (i.e., the new postage is the current postage + mrkdup_postage), but “if permit number is NA then the new postage should be the postage”.

In other words:

elif permit_num == 'NA':
    new_postage = postage

What if the permit number is neither ‘95’ nor ‘462’ nor ‘NA’?

Maybe you need to add:

else:
    new_postage = postage

or maybe you need it to complain that it doesn’t know what to do for the given permit number.

1 Like

Thank you Paul. I unfortunately am really new to python. I use more VDP programs where the coding is similar but very different. And if I get confused the VDP program I use techs will write the code for me.

I think I need more of a visual learning tool. Do you know of any of those.

Hi Matt,

Thank you. That actually worked. The 2 permits are our mailing permits that we do special pricing for and the NA is for stamps or international mailings.

Can I ask you more questions.

The tutorials that I included are actually relatively simple. They include block diagrams describing the expected behavior of the small code examples to aid in the understanding. Give them a try.