0
How to remove words and symbols from a string or ignore them? Then how to separate the present numbers in it?
How to remove words and symbols from a string or ignore them? Then how to separate the present numbers in it? For example: "19 years, 9 months and 8 days." I want to keep just the numbers, then turn every time unit for days, in way that I print my age in days like "7118 days." How to do it?
12 Respuestas
+ 1
If you want to pick out the integer numbers (and not just every digit), you can try this:
https://code.sololearn.com/cte2pk36EPZ6/?ref=app
This works for sunny days, but you have to be careful. What about if the number is a float, comes in different order, the numbers are more or less then you’d expected or if some number comes before a punctation mark? This is all case you have to consider if the input comes from an ordinary user.
(To calculate the days you need a calendar, because the number of days for a year and a moth is different for various years and months.)
+ 2
Hi! Maybe this can be a start for you:
https://www.google.se/amp/s/pythonexamples.org/JUMP_LINK__&&__python__&&__JUMP_LINK-regex-extract-find-all-the-numbers-in-string.amp/
+ 1
Thanks! To do it without import any libs is possible? How?
+ 1
Hi!
Well this is an example of course. Only you can specify how you want you program to work. But somehow you have to have some ways to relate the data in the input to your code.
When the degree of fredoom increase, codes to handle this type of situation pretty soon becomes complex. Thats why a form for how to input data makes it more easy avoid ambigutieties and errors.
+ 1
I really appreciated your effort, thanks a lot man, the point is that it isn't what I what I want but what the question Is asking for... But as you said it was an example, I understand you. Anyways I need a complete answer because I got stucked in this homework and need the answer to study the case, you know, huh?
+ 1
Okey, I understand. But what do you mean with “you attributed a value to the variables”? The data values (numbers) in the input string works with the conditions outpointed in the comments.
The numbers are taken from the string and are assigned to the list in the same way the the atricle about the regular expression explained, which I discussed earlier, but without importing the re module. It works!
There is no manual assignment there. The only ’manual’ (explicit) assignments to variables are the assignment of the constants (average days in a year and a month) intended for the calculation of the total number of days, but this is somthing completely different.
How do you want the program to work? Take the contditions in the comments as an starting point for your explaination:
• order on data in the input string;
• spaces around the data etc.
And: how would you know if a number in the input string refer to number of years, month and days, if you are alloweded to input the values in any order?
+ 1
ok, I got it now, I misunderstood but now I got! Thanks for the help!
+ 1
Man Per Bratthammar , you are so, so, so smart, professional hacker hahaha, for how long have you been programming in python?
+ 1
😁Hi, just take a one step at a time, and you will do great! /Regards Per B
0
Per Bratthammar , you attributed a value to the variables, It must apply for any data the person put in the input, in other words, the age musn't be attributed to a pre-existing variable.
0
How long to get in this level? Hahahah 😂
0
I don’t know how to count... I started my interest for programming a long time ago in the 80s (with BASIC and DOS 🙂). But in long periods I have done completely other things. When it comes to Python, maybe two, three years. But it’s just a hobby to me... I work in another field/area.