Home > Enterprise >  How I solve this error with the split function?
How I solve this error with the split function?

Time:07-25

I have written the following code and seem to be getting an error, can anyone help? I am using python 3.

Input code:

def fruits(h):
  ingredients = fruits.split()
  print(ingredients)

print(fruits("apples berries honey"])

Output:

    ingredients = fruits.split()
AttributeError: 'function' object has no attribute 'split'

CodePudding user response:

You are calling a method called split() with a function name. split() function only exists as a method of String type. Thus, you must call and invoke split() with a String type variable not a function.

Maybe, the argument h is a String. So you might want to do h.split()

And you also have the closing bracket, when you call

print(fruits("apples berries honey"])

please modify this code to down below:

print(fruits("apples berries honey"))
  • Related