Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract words from a sentence and check if a word is not there in it

this is my code below. I need to take an input from the user (a sentence) and check whether there are any numbers in it from 0 to 10 in it or not. I know there are many ways to approach it, e.g. split(), isalnum(), etc. but I just need help in putting it all together. Please find my code below:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1","2","3","4","5","6","7","8","9","10","0"]

ss1 = sentence1.split()
print(ss1)
if numbers in ss1:
  print("There are numbers between 0 to 10 in the sentece")
else:
  print("There are no numbers in the sentence”)

Thanks :)

Edit: Expected Output should be like:

Enter any sentence (it may include numbers): I am 10 years old
There are numbers between 0 and 10 in this sentence
like image 301
Arnav_8 Avatar asked Dec 10 '25 04:12

Arnav_8


2 Answers

I would use a regex:

def contains_nums(s):
    import re
    m = re.search('\d+', s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        return f'"{s}" contains no numbers'

Examples:

>>> contains_nums('abc 10')
'"abc 10" contains 10'

>>> contains_nums('abc def')
'"abc def" contains no numbers'

NB. this is only checking the first number, use re.findall if you need all. Also this is finding numbers within words, if you want separate numbers only, use a regex with word boundaries (\b\d+\b), finally, if you want to restrict to 0-10 numbers, use (?<!\d)1?\d(?!\d) (or \b(?<!\d)1?\d(?!\d)\b for independent numbers)

more complete solution
def contains_nums(s, standalone_number=False, zero_to_ten=False):
    import re
    
    regex = r'(?<!\d)1?\d(?!\d)' if zero_to_ten else '\d+'
    
    if standalone_number:
        regex = r'\b%s\b' % regex
    
    m = re.search(regex, s)
    if m:
        return f'"{s}" contains {m.group()}'
    else:
        a = "standalone " if standalone_number else ""
        b = "0-10 " if zero_to_ten else ""
        return f'"{s}" contains no {a}{b}numbers'
>>> contains_nums('abc100', standalone_number=False, zero_to_ten=False)
'"abc100" contains 100'

>>> contains_nums('abc100', standalone_number=True, zero_to_ten=False)
'"abc100" contains no standalone numbers'

>>> contains_nums('abc 100', standalone_number=True, zero_to_ten=True)
'"abc 100" contains no standalone 0-10 numbers'
like image 157
mozway Avatar answered Dec 11 '25 17:12

mozway


Try this solution:

sentence1 = input("Enter any sentence (it may include numbers): ")
numbers = ["1","2","3","4","5","6","7","8","9","10","0"]
verif = False
for n in numbers:
    if n in sentence1:
        verif = True
        break
if verif:
    print("There are numbers between 0 to 10 in the sentence")
else:
    print('There are no numbers in the sentence')

output:

Enter any sentence (it may include numbers): gdrsgrg8
There are numbers between 0 to 10 in the sentence
like image 34
Thekingis007 Avatar answered Dec 11 '25 17:12

Thekingis007



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!