Getting wrong output when working with tuple and list in Python -
trying learn python , have following task:
- get phrase user input.
- check if input contains consonant consonants tuple\list declare in code.
- 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
Post a Comment