Getting wrong output when working with tuple and list in Python -


trying learn python , have following task:

  1. get phrase user input.
  2. check if input contains consonant consonants tuple\list declare in code.
  3. for every consonant in user input, print consonant followed letter 'o' , consonant itself.

for example:

  • user types word 'something' input
  • output should be: 'sosomometothohinongog' (vowels not exist in consonant tuple , hence not being appended).

this code:

#!/usr/bin/python  consonant = ('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z')  def isconsonant(user):     consonant in user:         print consonant + "o" + consonant  var = raw_input("type smth: ") isconsonant(var) 

here get:

root@kali:~/py_chal# ./5.py type smth: test tot eoe sos tot 

i have trouble with:

  • the code treats vowels consonants though not in list (notice 'e').
  • the 'print' method adds new line - solved importing sys module , using 'write'.

any tips appreciated.

consonant = ('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z') def isconsonant(user):     letter in user:         if letter in consonant:             print letter + "o" + letter  var = raw_input("type smth: ") isconsonant(var) 

or

consonant = ('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z') print "\n".join(["%so%s" % ( letter, letter) letter in raw_input("type smth: ") if letter in consonant]) 

or maybe

print "\n".join(["%so%s" % (l,l) l in set(raw_input("type smth: ")).difference('aeiou')]) 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -