简体   繁体   中英

Python command line arguments in class not working

I am accepting two strings word1 and word2 , through command line which should contain alphabets only.

The check_conditions function will make sure strings are for alphabets only.

When I run this program as python myprogram.py 'word1' 'word2' , it does not show anything, no error, no output.

When I run this program without using class, it works as excepted.

 class FindWinner:
    def __init__(self, word1, word2):
        self.word1 = word1
        self.word2 = word2

def check_conditions(self):
    special_chars = set('[~!@#$%^&*.-()_+{}":;01234567989\']+$')
    if special_chars.intersection(self.word1) or special_chars.intersection(self.word1) or (len(self.word1) != len(self.word2) 
or ('no' in self.word1) or ('no' in self.word2)):
        print('Invalid string.')
    else:
        print("String is valid")
        print(list(self.word1))
        print(list(self.word2))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("word1", help="First word")
    parser.add_argument("word2", help="Second word")
    args = parser.parse_args()

    c1 = FindWinner(args.number1, args.number2)
    c1.check_conditions()

What am I missing here?

I missed to call main() 我错过了打电话给main()

if __name__ == "__main__":
    main()

Now, I am getting another error:

if special_chars.intersection(self.word1) or special_chars.intersection(self.word1) or (len(self.word1) != len(self.word2) or ('no' in self.word1) or ('no' in self.word2)):

When I run python myprogram.py my_string my_long_string , it gives me string is valid output(else part). As per above condition, both strings should not contain any special char & len should be same.

You should call the main function from your code since you have only defined it not called it. The usual way to get this done by adding the following lines at bottom of your code.

if __name__ == '__main__':
    main()

To expound on @sdvd's answer, the lines

if __name__ == “__main__”:
    main()

Checks if the script is being invoked directly by the interpreter or if it is being imported. If the script is being imported, the main function is not run, because chances are you don't want to run the script immediately. If the script is being called directly by the interpreter, you want to run the main function immediately, which is what happens when that expression evaluated to true.

In your script now, there is no call to the main function, so nothing happens. Add the above lines to the bottom of your script, and things should proceed as you want them to.

To access your arguments, use args.word1 and args.word2 instead of args.number1 and args.number2 because that's what you named them.

You name the argument one thing and use it later with another name.

Try using args.word1 and args.word2 , or call them number1 and number2 when you add them (using add_argument ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM