Home python Count the number of elements and display

Count the number of elements and display [duplicate]

Author

Date

Category

The user must set the sequence himself.
A character string is given; count how many times among the characters in the string
the letter “x” is encountered.
In principle, the task is not difficult, I just can’t figure out what doesn’t work here:

Divide the sequence into individual elements, then set the condition that when this element is encountered, I use count to count the number of elements.

n = list ()
n = input ("Enter the sequence:")
n.split ()
i = 0
for i in n:
  i + = 1
  if (i == 'x'):
    s.count ('x')
print (n)

Answer 1

You first write n = list () and then the same n becomes input () , you assign different values ​​to one variable. If you want to wrap the entered string in a list, you can do list (input (). Split ())
Or like this, if you enter a sequence separated by a space:

n = [i for i in input ("Please enter a sequence:") .split ()]
x = n.count ('x')
print (x)

Or easier, if you enter any line:

n = input ("Enter the sequence:")
x = n.count ('x')
print (x)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions