python if statement with multiple conditions, and or operator precedence

307
November 23, 2017, at 06:34 AM

my original code had no parenthesis around the ... or ... parts of the statements and nothing worked as intended.

The use of -a printed -a (not wanted) and all arguments twice.

The use of -b gave the 2nd if statement of "error: enter filename"

I was referred to this link http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html and thought I understood operator preference.

Thought the brackets would make it check for ==2 and -a, then ==2 and -b and so on.

#!/usr/bin/env python3
import sys
if len(sys.argv) ==1:
    print ("\nerror: please enter at least one filename\n")
    sys.exit()
if len(sys.argv) ==2 and (sys.argv[1]== "-a" or sys.argv[1] == "-b"):
    print ("\nerror: please enter at least one filename after -a or -b\n")
    sys.exit()

if len(sys.argv) >=2 and (sys.argv[1] != "-a" or sys.argv[1] != "-b"):
    for arg in sys.argv[1:]:
        print(arg)

if len(sys.argv) >=2 and (sys.argv[1] == "-a" or sys.argv[1] == "-b"):
    for name in sys.argv[2:]:
        print(name)

However now the use of -a or -b gives the original -a result (prints the -a / -b and also prints all arguments twice).

The use of break stops the repeat but I have no idea why the -a / -b continues to be printed. Is the use of slice [2:] not enough?

Thank you for your help.

Rent Charter Buses Company
READ ALSO
How do I get an image to move until it gets to a specific point in the window?

How do I get an image to move until it gets to a specific point in the window?

Right now I have some code in Python using Zelle graphics that has a robot image and some text scrolling upwardWhen the user clicks on the window, the program ends

220
trouble with a for loop inside a while loop

trouble with a for loop inside a while loop

I'm trying to write some code that will find the roots of a polynomial by cycling through various starting values (x0) and running the Newton-Raphson method for each value of x0

223
Removing objects from lists

Removing objects from lists

I'm not sure why I get an error saying index out of range when I test this functionCan anyone please help me fix this?

273
How can I append element to the DataFrame with this data?

How can I append element to the DataFrame with this data?

How can I add last element to DataFrame

258