This is only my second Python script, so let me have it! If anything seems off in style or implementation, please nit pick away. I want to learn. I know it could've been done in less lines of code without making separate functions and whatnot, but I wanted to approach it like I would an actual project.
#!/bin/usr/python
from sys import exit
def print_help():
print("")
print("--------------------------------")
print(" Command | Description")
print("--------------------------------")
print(" -h | Print help list")
print(" -x | Exit program")
print("--------------------------------")
print("")
def is_valid_word(word):
return word and word.isalpha()
def is_valid_string(string):
for word in string.split(" "):
if not is_valid_word(word):
return False
return True
def get_pig_latin(string):
vowels = "aeiou"
built = ""
for word in string.split(" "):
if len(word) < 2 or word[0] in vowels:
built += word + "tay"
else:
temp = word
while temp[0] not in (vowels + "y"):
temp = temp[1:] + temp[0]
built += temp + "ay"
built += " "
built = built[0].upper() + built[1:]
return built
def print_title():
print("")
print("--------------------------------")
print("----- PIG LATIN TRANSLATOR -----")
print("--------------------------------")
print("Type -h for help")
print("")
def run():
commands = { "-h" : print_help,
"-x" : exit }
print_title()
while True:
input = raw_input(":: ").strip().lower()
if input in commands:
commands[input]()
elif is_valid_string(input):
print("-> " + get_pig_latin(input) + "\n")
else:
print(" Unrecognized word or command.\n")
if __name__ == "__main__":
run()
raw_input
is in Python2 not Python3 \$\endgroup\$is_valid_string()
with theall()
function. \$\endgroup\$word and word.isalpha()
can be replaced withword.isalpha()
\$\endgroup\$raw_input()
? \$\endgroup\$