Deleted


#1

i dont need help thanks a lot


#3

i would do the following, create functions for the different options:

def option_1:
    # word to list function
def option_2:
    # letter count

and so on. Then create a menu function, which prints the different options

a main function, which does the following:

calling the menu function
a list with all the option functions
prompt the user for a number (and subtract one, given lists are zero indexed based)
from the list, call the right option function (which is why subtracting one is important, given humans starts counting at 1)

verify that: the user enters a valid number (what is the user enters a string, for example'test' then float(input()) will throw an error, you need to handle this

also verify the user enters is in list, before accessing the list, otherwise you also get an error


#4

A post was split to a new topic: Ruby vs python


#5

For something as trivial and repeated as a main menu we want to minimize the code bloat by taking advantage of Python’s printable comments, similar to PHP’s heredoc, though not quite as complex and strict.

def menu():
  print ("""
  Hello, please select one of the options from this menu:
  
  > 0 -- Exit
  
  > 1 -- Enter a word
  
  > 2 -- Check occurence of a letter
  
  > 3 -- Palindrome
  
  > 4 -- Anagram
  """)
  return ">>>"

Now combining it with your input statement:

def main_menu():
    option = input(menu())
    if option == 0: return "Bye"
    # ...

main_menu()

Using your model (code is contained in the control flow structure) I came up with this example…

https://repl.it/NbLA

Be sure to enter 0 or the program keeps running.

 > 

  Select an option from the menu:
  > 0 -- Exit
  > 1 -- Reverse a word
  > 2 -- Check occurence of a letter
  > 3 -- Palindrome
  > 4 -- Anagram
  
>>> 3
Please enter any word: 
>>> aibohphobia
==> True

 > 

  Select an option from the menu:
  > 0 -- Exit
  > 1 -- Reverse a word
  > 2 -- Check occurence of a letter
  > 3 -- Palindrome
  > 4 -- Anagram
  
>>> 4
Enter word or phrase one: 
>>> anagram
Enter word or phrase two: 
>>> nag a ram
==> True

#7

It should be mentioned that the convention in the programming community is to not capitalize variable names. Capitalization denotes a class . Use lowercase so your code is easier for others to read and it follows more the norm.

def count_letters(word, letter):

I rather doubt that @stetim94 meant it to literally use option_1, etc. as function names. The name of a function should describe what it does or returns, similar to your function name above.

def is_palindrome(word_phrase):

def is_anagram(phrase_one, phrase_two):

The names suggest the functions will return a boolean, True if the test is met.

One last proviso, we should avoid using reserved words as variable names, including inside a function. list is a built in function.


#8

@mtf pretty much covered what i meant to say,

my answer was only showing how to design your program, you shouldn’t use those variable names


#9

This is an emulation of what Steven alluded to earlier…

def menu():
  print ("""
  Select an option from the menu:
  > 0 -- Exit
  > 1 -- Reverse a word
  > 2 -- Check occurence of a letter
  > 3 -- Palindrome
  > 4 -- Anagram
  """)
  return ">>>"

def reverse_string():
  gWord = input("Please enter any word: \n>>>")
  letters = []
  for letter in gWord:
    letters.insert(0, letter)
  return '==> {}'.format(''.join(letters))
  
def char_count():
  needle = input("Enter any letter to count in the word you previously entered:")
  count = 0
  for letter in gWord:
    count += (needle == letter)
  return '==> {}'.format(count)

def is_palindrome():
  word = input("Enter a word or phrase: \n>>>").lower()
  return '==> {}'.format(word == word[::-1])
  
def is_anagram():
  def word_(x):
    return list(input("Enter word or phrase {}: \n>>>".format(x)).lower())
  def crunch(x):
    return ''.join(sorted(x)).strip()
  return '==> {}'.format(crunch(word_('one')) == crunch(word_('two')))

gWord = ""
def main_menu():
  global gWord    # keep user input alive when recursing the function
  option = input(menu())
  if option == 0: return "Bye"
  if option == '1':
    print (reverse_string())
    main_menu()
  elif option == '2':
    print (char_count())
    main_menu()
  elif option == '3':
    print (is_palindrome())
    main_menu()
  elif option == '4':
    print (is_anagram())
    main_menu()
        
main_menu()

Now we can see explicitly the duplication in our code and discern the patterm. A shorter solution is possible.

https://repl.it/NbLA/1


#10

Now we get to refactor the main_menu function. Error checking will come in later, so invalid input will raise an exception at present.

def main_menu():
  option = input(menu())
  if option == 0: return "Bye"
  print (['',reverse_string,char_count,is_palindrome,is_anagram][int(option)]())
  main_menu()
   
gWord = ""    # global shared by two functions
main_menu()

That gets rid of the repetition from earlier. Note the objects in the anonymous list are not strings, but identifiers. We match up the identifier to the option selected and invoke it with ().

https://repl.it/NbLA/3