Home python How to calculate the amount of numbers in the line

How to calculate the amount of numbers in the line

Author

Date

Category

Dana row, items of which are any characters, how to calculate the amount of numbers in the line

Follow the code

STR1 = STR (INPUT ())
Print (STR1)
k = 0.
For Elem in Str1:
  IF Elem & GT; = "0" and Elem & Lt; = "9":
    k = k + 1
    Print (Elem)
Print ("k =", k)

Answer 1

You need to check whether it is possible to make a number from the symbol, this is done like this:

elem.isdigit ()

you need to find the amount and not the quantity:

k = k + 1

Change on:

k + = int (elem)

code should be:

STR1 = STR (INPUT ())
Print (STR1)
k = 0.
For Elem in Str1:
  IF Elem.isdigit ():
    K + = int (Elem)
    Print (Elem)
Print ("k =", k)

Answer 2

import re
a = input ('')
Digits = Re.Findall ("\ d", a)
IF Not Digits:
  Print ('numbers not found')
ELSE:
  Print (Eval ('+'. Join (Digits)))

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