# -*- coding: utf-8 -*- # Goals: learn how to accept input from the command line (e.g. python 6.py "A String") # learn how to write a one-line for loop with an if statement import sys import getopt import string def usage(): print "String Disemvoweler:" print "Syntax: python 6.py [options] string" print print "Options:" print "-c: print only the consonants in the string (default option)" print "-v: print only the vowels in the string" print print "These options are mutually exclusive." sys.exit(2) try: opts, args = getopt.getopt(sys.argv[1:], "cv") except getopt.GetoptError: usage() if len(opts) > 1: usage() elif len(opts) < 1: option = "-c" else: option = opts[0][0] thestring = " ".join(args) if thestring.strip() == "": usage() sys.exit(2) vowels = "aeiouAEIOU" consonants = "".join(char for char in string.letters if not char in vowels) vowels = vowels + string.punctuation + string.whitespace consonants = consonants + string.punctuation + string.whitespace if option == "-c": print "".join(char for char in thestring if char in consonants) else: print "".join(char for char in thestring if char in vowels)