Pahan
Pahan

Reputation: 68

getting this error called TypeError: count() takes at least 1 argument (0 given)

This program calculates the number of letters in a str input. If the user types 'ns' it prints the number of letters without the spaces

This is the code

name = input('Enter a sentence to calculate the number of letters: \n')
counter = str(len(name))


print(counter, ": with spaces.")
spaces = input("Type 'ns' to calculate the letters without the spaces \n")
counter_without_spaces = str(str.count(name))
if spaces == 'ns':
    print(counter_without_spaces)



input()

This is the error in the terminal

TypeError: count() takes at least 1 argument (0 given)

Upvotes: 0

Views: 2033

Answers (3)

itsDV7
itsDV7

Reputation: 854

Here's one of the things you can do:

name = input('Enter a sentence to calculate the number of letters: \n')
counter = str(len(name))


print(counter, ": with spaces.")
spaces = input("Type 'ns' to calculate the letters without the spaces \n")
counter_without_spaces = str(name.count(" "))
if spaces == 'ns':
    print(str(int(counter) - int(counter_without_spaces)))

Another Way:

name = input('Enter a sentence to calculate the number of letters: \n')
counter = str(len(name))
print(counter, ": with spaces.")
spaces = input("Type 'ns' to calculate the letters without the spaces \n")
if spaces == 'ns':
  print(str(len(name)-name.count(" ")))

Output:

Enter a sentence to calculate the number of letters: 
abc def
7 : with spaces.
Type 'ns' to calculate the letters without the spaces 
ns
6

About count():

The string count() method returns the number of occurrences of a substring in the given string.

In simple words, count() method searches the substring in the given string and returns how many times the substring is present in it.

Upvotes: 3

Moetaz Brayek
Moetaz Brayek

Reputation: 269

this will work

answer = input("with space or without  yes/no")
word = input("enter word : ")
if str(answer) == "no":
    print(len(word))
else:

    print(len(word.replace(" ", "")))

Upvotes: 1

Prune
Prune

Reputation: 77900

You failed to tell it what to count. You called count through the class, which means you have to give it both the string and the character you want to count within that string. When it got to the actual count function, it had already converted your call into its canonical form:

name.count()

... where you're missing the required argument.

You need to return to the materials on strings and the count method in particular. Your program logic shows that you aren't clear on how it works. You need to count the spaces and subtract that from the total string length ... both of which you now know how to do.

Upvotes: 0

Related Questions