1

Trying to learn some python and I have the following task:

  1. Get a phrase from the user as an input.
  2. Check if the input contains a consonant from a consonants tuple\list that I declare in the code.
  3. For every consonant in the user input, print the consonant followed by the letter 'o' and the consonant itself.

For example:

  • User types the word 'something' as an input
  • Output should be: 'sosomometothohinongog' (vowels do not exist in the consonant tuple and hence are not being appended).

This is my 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):
    for consonant in user:
        print consonant + "o" + consonant

var = raw_input("type smth: ")
isConsonant(var)

Here is what I get:

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

I have trouble with:

  • The code treats vowels as consonants even though they are not in the list (notice the 'e').
  • the 'print' method adds a new line - This was solved by importing the sys module and using 'write'.

Any tips are greatly appreciated.

1
  • 2
    You're being confused by two usages of in. It means different things in a for loop vs. an if statement. Apr 16, 2014 at 15:40

2 Answers 2

3
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):
    for 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) for letter in raw_input("type smth: ") if letter in consonant])

OR maybe

print "\n".join(["%so%s" % (l,l) for l in set(raw_input("type smth: ")).difference('aeiou')])
3
  • Thanks bsoist, that solved it. Can you explain the changes of behavior between the 2 code snippets? Having a difficulty grasping what was wrong in my code.
    – Franko
    Apr 16, 2014 at 15:47
  • Someone else pointed out something - two different ways to use 'in' - which might explain why you are reassigning consonant. You set it to a tuple, but then reset it to each letter in the word the user types in ( note my change to the variable name ). You also never check to see if the letter you are examining is in the tuple, which you can't do anyway once you've erased it. :)
    – bsoist
    Apr 16, 2014 at 15:53
  • 1
    @Franko your loop for consonant in user was creating a new variable consonant for each letter of user instead of testing against the global as you intended. Apr 16, 2014 at 15:53
1

You can easily print without adding a new line at the end without importing anything by adding a , at the end of print like this:

print 'hello ',
print 'world',
print '!'
1
  • And when you switch to Python 3, use end=''. Apr 16, 2014 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.