Skip to main content
added 159 characters in body
Source Link
AChampion
  • 476
  • 2
  • 9

Parens are unnecessary and you could just call the arg original andas you don't really need this assignment.

def input_state():

    sentence = input("enter a word to be translated: ")

Assume you meant to ask for a words or a sentence given the variable name:

    list = sentence.split()

You should look into list comprehensions and generators as this couldcan be significantly simplified, e.g. print(" ".join(trans_one_word(i) for i in sentence.split()))

    print (s.join( new_list ))

 

It's probably better to return this result and let the caller print it.

input_state()

another = input("Would you like to enter another sentence to be     translated? \n Y/N")

if (another == 'y') or (another == 'Y'):
    input_state()
def trans_one_word(word):
    pyg = 'ay'

    if word and word.isalpha():
        word = word.lower()
        return word[1:] + word[0] + pyg
    return word

def input_state():
    sentence = input("enter a wordsentence to be translated: ")
    return " ".join(trans_one_word(word) for word in sentence.split())

while True:
    print(input_state())
    another = input("Would you like to enter another sentence to be translated?\nY/N")
    if another not in 'yY':
        break

Parens are unnecessary and you could just call the arg original and you don't really need this assignment.

def input_state():

    sentence = input("enter a word to be translated: ")
    list = sentence.split()

You should look into list comprehensions this could be significantly simplified.

    print (s.join( new_list ))

 
input_state()

another = input("Would you like to enter another sentence to be     translated? \n Y/N")

if (another == 'y') or (another == 'Y'):
    input_state()
def trans_one_word(word):
    pyg = 'ay'

    if word and word.isalpha():
        word = word.lower()
        return word[1:] + word[0] + pyg
    return word

def input_state():
    sentence = input("enter a word to be translated: ")
    return " ".join(trans_one_word(word) for word in sentence.split())

while True:
    print(input_state())
    another = input("Would you like to enter another sentence to be translated?\nY/N")
    if another not in 'yY':
        break

Parens are unnecessary and you could just call the arg original as you don't need this assignment.

def input_state():

    sentence = input("enter a word to be translated: ")

Assume you meant to ask for a words or a sentence given the variable name:

    list = sentence.split()

You should look into list comprehensions and generators as this can be significantly simplified, e.g. print(" ".join(trans_one_word(i) for i in sentence.split()))

    print (s.join( new_list ))

It's probably better to return this result and let the caller print it.

input_state()

another = input("Would you like to enter another sentence to be     translated? \n Y/N")

if (another == 'y') or (another == 'Y'):
    input_state()
def trans_one_word(word):
    pyg = 'ay'

    if word and word.isalpha():
        word = word.lower()
        return word[1:] + word[0] + pyg
    return word

def input_state():
    sentence = input("enter a sentence to be translated: ")
    return " ".join(trans_one_word(word) for word in sentence.split())

while True:
    print(input_state())
    another = input("Would you like to enter another sentence to be translated?\nY/N")
    if another not in 'yY':
        break
Source Link
AChampion
  • 476
  • 2
  • 9

Some comments on your code. Overall you are using a lot of unnecessary variables.

another_word = " "
sentence1 = " "
new_list = []
another = " "

You really don't need these variables declared outside of your functions, they add no value and just pollute the namespace.

def trans_one_word (i):

PEP8 doesn't recommend a before the args. And you should consider having more meaningful arg names.

    global another_word

Unnecessary global, you are returning this variable from the function so don't need it to be global

    pyg = 'ay'

    original = (i)

Parens are unnecessary and you could just call the arg original and you don't really need this assignment.

    if len(original) > 0 and original.isalpha():
        word = original.lower()
        first = word[0]
        new_word = word + first + pyg
        another_word = new_word[1:len(new_word)]

You don't need len(new_word) on a slice an empty slice implies to the end of the word, e.g. new_word[1:]

        first = word[0]
        new_word = word + first + pyg
        another_word = new_word[1:len(new_word)]
        return another_word

This whole block can be simplified to return word[1:] + word[0] + pyg

    else:
        print('empty')

You don't need the else: clause because the previous block returned. You should also return the original word.

def input_state():

    sentence = input("enter a word to be translated: ")
    list = sentence.split()

list is a python type, so shouldn't be used for a variable name.

    for i in list:
        trans_one_word(i)
        new_list.append(another_word)
    s = " "

You should look into list comprehensions this could be significantly simplified.

    print (s.join( new_list ))


input_state()

another = input("Would you like to enter another sentence to be     translated? \n Y/N")

if (another == 'y') or (another == 'Y'):
    input_state()

This condition can be replaced with if another in 'yY': You should really put this all into a loop so with an exit clause.

Simplifying the code:

def trans_one_word(word):
    pyg = 'ay'

    if word and word.isalpha():
        word = word.lower()
        return word[1:] + word[0] + pyg
    return word

def input_state():
    sentence = input("enter a word to be translated: ")
    return " ".join(trans_one_word(word) for word in sentence.split())

while True:
    print(input_state())
    another = input("Would you like to enter another sentence to be translated?\nY/N")
    if another not in 'yY':
        break