Expanded Translator for multiple consonants/vowels etc, Please critique

Hello,

I wanted to make a translator that works with words that start with 1/2/3/4 vowels or words that start with 1/2/3/4 consonants, words that use y as a vowel or a consonant. Like style, schlep, chrysolite, phyryganeides, boots, and the like.

It’s a lot of code but could you test it to see if there are any errors. I tried to cover all my bases but im sure I missed something.

Thank you for your time.

cpyg = "ay"
vpyg = "way"
vowels = "aeiou"
cons = "bcdfghjklmnpqrstvwxyz"

original = raw_input("Enter a word: ")

if original[0] in vowels:
	new_word = original + vpyg
	print new_word

elif original[0] != "y":
	vowels = "aeiouy"
	cons = "bcdfghjklmnpqrstvwxz"
	if original[0] in cons and original[1] in cons and original[2] in cons and original[3] in cons:
		start = original[0:4]
		new_word = original[4:len(original)] + start + cpyg
		print new_word
	elif original[0] in cons and original[1] in cons and original[2] in cons:
		start = original[0:3]
		new_word = original[3:len(original)] + start + cpyg
		print new_word
	elif original[0] in cons and original[1] in cons:
		start = original[0:2]
		new_word = original[2:len(original)] + start + cpyg
		print new_word
	elif original[0] in cons:
		if original[0] == "q" and original[1] == "u":
			start = original[0:2]
			new_word = original[2:len(original)] + start + cpyg
			print new_word
		else: 
			start = original[0:1]
			new_word = original[1:len(original)] + start + cpyg
			print new_word
	else:
		start = original[0:1]
		new_word = original[1:len(original)] + start + cpyg
		print new_word

elif original[0] in cons and original[1] in cons and original[2] and original[3] in cons:
	start = original[0:4]
	new_word = original[4:len(original)] + start + cpyg
	print new_word 
elif original[0] in cons and original[1] in cons and original[2] in cons:
	start = original[0:3]
	new_word = original[3:len(original)] + start + cpyg
	print new_word 
elif original[0] in cons and original[1] in cons:
	start = original[0:2]
	new_word = original[2:len(original)] + start + cpyg
	print new_word 
elif original[0] in cons:
	if original[0] == "q" and original[1] == "u":
		start = original[0:2]
		new_word = original[2:len(original)] + start + cpyg
		print new_word
	else:
		start = original[0:1]
		new_word = original[1:len(original)] + start + cpyg
		print new_word		
else:
	print "Please enter a valid word."	

For efficiency, does it not make more sense to wait for print until after you’ve checked everything? General rule of thumb is don’t repeat yourself… There’s a bunch of repetition with prints here!

I’ve made a quick update to your code to reduce repetition. Here’s what I came up with:

cpyg = "ay"
vpyg = "way"
vowels = "aeiou"
cons = "bcdfghjklmnpqrstvwxyz"

original = str(input("Enter a word: ")) #Specify input type for our sanity

new_word = "" #Setting it to a blank string so we can use it later

if original[0] in vowels:
	new_word = original + vpyg
elif original[0] != "y":
	vowels = "aeiouy"
	cons = "bcdfghjklmnpqrstvwxz"
	if original[0] in cons and original[1] in cons and original[2] in cons and original[3] in cons:
		start = original[0:4]
		new_word = original[4:len(original)] + start + cpyg
	elif original[0] in cons and original[1] in cons and original[2] in cons:
		start = original[0:3]
		new_word = original[3:len(original)] + start + cpyg
	elif original[0] in cons and original[1] in cons:
		start = original[0:2]
		new_word = original[2:len(original)] + start + cpyg
	elif original[0] in cons:
		if original[0] == "q" and original[1] == "u":
			start = original[0:2]
			new_word = original[2:len(original)] + start + cpyg
		else: 
			start = original[0:1]
			new_word = original[1:len(original)] + start + cpyg
	else:
		start = original[0:1]
		new_word = original[1:len(original)] + start + cpyg

elif original[0] in cons and original[1] in cons and original[2] and original[3] in cons:
	start = original[0:4]
	new_word = original[4:len(original)] + start + cpyg
elif original[0] in cons and original[1] in cons and original[2] in cons:
	start = original[0:3]
	new_word = original[3:len(original)] + start + cpyg
elif original[0] in cons and original[1] in cons:
	start = original[0:2]
	new_word = original[2:len(original)] + start + cpyg
elif original[0] in cons:
	if original[0] == "q" and original[1] == "u":
		start = original[0:2]
		new_word = original[2:len(original)] + start + cpyg
	else:
		start = original[0:1]
		new_word = original[1:len(original)] + start + cpyg
else:
	print ("Please enter a valid word.")	

print (new_word) #If error, just prints blank space... No effect.

This code is also updated with Syntax for Python3 (parenthesis call to print, changed raw_input to str(input())…)

See/run the new code on repl.it.

Looks good so far! Keep going!

3 Likes

For a more general (and readable) approach, you can look for the first vowel and then split the string there, the resulting code should be pretty short.

That’s how you’d do it manually, your code shouldn’t be any different.

import re

original = input('Enter a word: ')
# split the string at each vowel, keep the first section (leading non-vowels)
leading_consonants, *rest = re.split('([aoeui])', original)
# put the rest back together into a single string
rest = ''.join(rest)
print(f'{leading_consonants!r} {rest!r}')

(python3.6+)

Enter a word: schtick
'scht' 'ick'

A plain loop would do just fine to get the location, and then slice it. If you care about 'qu' as a special case you can easily scoot leading u’s over after this split

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.