Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Gary Gibson
Gary Gibson
5,011 Points

Still not clear on when to add arguments in parentheses.

I thought I understood when to add arguments to a function, but I don't. For example, this works:

def devowel():
    word = input("Give me a word to devowel. ")
    vowels = 'aeiou'
    result = ''
    for i in word:
        if i not in vowels: 
            result += i
    print(result)

devowel()

But this throws an error:

def devowel(word):
    word = input("Give me a word to devowel. ")
    vowels = 'aeiou'
    result = ''
    for i in word:
        if i not in vowels: 
            result += i
    print(result)

devowel(word)

This works too, though:

def devowel(word):
    word = input("Give me a word to devowel. ")
    vowels = 'aeiou'
    result = ''
    for i in word:
        if i not in vowels: 
            result += i
    print(result)

devowel(abomination)

So when the two arguments look different, it's fine.

What's going one?

2 Answers

Hi Gary

I ran both your examples and they both throw errors, reason being the argument you are passing is not a string. Hence the error you get is word is not defined or abomination is not defined.

def devowel(word):
    word = input("Give me a word to devowel. ")
    vowels = 'aeiou'
    result = ''
    for i in word:
        if i not in vowels: 
            result += i
    print(result)

devowel("abomination") # surround either word or abomination within quotes as you need to pass a string.

In terms of passing arguments, You pass arguments to a function to prevent repeating yourself. Imagine if i wanted to add 2 numbers, I would not say num1= 5+5 and then 20 lines down the code say again num3 = 25 +32. That's where functions come in. I could create a function named add and call it every time i need to add two numbers.

see below

def add(a,b):
    return a + b

#So when ever i need to add two numbers i call  num1=add(5,5) or num3 = add(25,32).

For this example i would not pass arguments as the input function will give you unique inputs from the user every time devowel function is called.

You first example is the best way to write the devowel function

def devowel():
    word = input("Give me a word to devowel. ")
    vowels = 'aeiou'
    result = ''
    for i in word:
        if i not in vowels: 
            result += i
    print(result)

devowel()

hope this helps

Gary Gibson
Gary Gibson
5,011 Points

Edit: Got it.

def devoweled(this_string):
    result = ''
    vowels = 'aeiou'
    for i in this_string:
        if i not in vowels:
            result += i 
    print(result)

devoweled("This String")

what you have written is fine. if you still require the user input then see below.

def devowel(word):
  vowels = 'aeiou'
  result = ''
  for i in word:
    if i not in vowels:
      result += i
  print(result)

user_word = input("Please enter a word to devowel! ")
devowel(user_word)
Gary Gibson
Gary Gibson
5,011 Points

Thank you, Andreas. Good illustration.