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://ift.tt/2zuSt6O 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.
Aucun commentaire:
Enregistrer un commentaire