Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python elementary problem
#1
In a given text you need to sum the numbers. Only separated numbers should be counted. If a number is part of a word it shouldn't be counted.

this is the problem. I'm a beginner programmer in terms of solving python logical problems. I've tried to use .isdigit but then I can't find if the numbers is separated or not. Now what must I do to solve the problem?

oh P.S: The text consists from numbers, spaces and english letters. No other signs.
Reply
#2
show what you've tried
Reply
#3
What does it mean to be a "separated number". Most of the separators will be spaces. Do you have any instructions on what to do about punctuation? Is "2020" a separated number in 'The sign is supposed to say "2020"'? Is '2020.' a separated number in 'This year is 2020.'? Do you need to find float numbers? Is 123.456 a separated number? Two separated numbers? Not separated?

And then think about how you will search. You could break your text up into "separated" things and find which of those are numbers, you you could search the text for any digit and then check if the digit is part of a separated number.

I would not be thinking about using "isdigit" at all and I would certainly not be thinking about it or any other python code until I had firmed up the logic of how I would solve the problem. If you cannot solve the problem using pencil and paper and a well defined set of instructions you cannot write a program.
ndc85430 likes this post
Reply
#4
(Nov-04-2020, 04:24 PM)Axel_Erfurt Wrote: show what you've tried
for words in x:
	if x.isnumeric:
		print(words)
	else:
		print(0)
here is what i've tried.
Reply
#5
A well placed print statement might help you see a problem. Insert print(words) above line 2.

Also note that x does not change, so checking it every time to see if it isnumeric won't help.
Reply
#6
maybe you mean

for words in x:
    if words.isnumeric:
        print(words)
    else:
        print(0)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020