2

So what i want is for my code to understand if my input has a number in it or not, if it does it is meant to output "Correct" but if it doesn't then it would output "Incorrect" how can i actually make this work. So it knows if there is an integer in the input or not. Any help is appreciated

import re
yourString=input()
number = re.search(r'\d+', yourString).group()
if number == True:
  print("Correct")
else:
  print("Incorrect")
1
  • do you want to know if there is a digit in your string or if you string is an integer? you have the correct (well, mostly correct) code for contains an integer Commented May 17, 2018 at 22:43

3 Answers 3

2

According to https://docs.python.org/3/library/re.html#match-objects you have a bit of a subtle error.

The statement should not be if number == True: it should be if number:, the reason being the value number is tests as True if anything is matched, and is None if there are no matches.

i.e.

import re
yourString=input()
number = re.search(r'\d+', yourString)
if number:
    print("Correct")
    group = number.group()
else:
    print("Incorrect")

Note, somewhat bizarrely

import re
yourString=input()
number = re.search(r'\d+', yourString)
if number:
    print(number == True)
    group = number.group()
else:
    print("Incorrect")

Prints false, so number is not the value true, it overrides __bool__. See https://docs.python.org/3/reference/datamodel.html#object.bool for more information

Sign up to request clarification or add additional context in comments.

4 Comments

ok thank you so much this is what i have been trying to figure out for a few days now and you have solved a giant problem for me thank you soooo much
Oops, if nothing returns, the group method will error. I have modified this again.
yeah i was going to ask a question about that but you just solved it for me thanks
Note, while if number: resolves to true, even in that block if you add print number == True that will print false, but the test function of the number object has been overridden. This is unusually complex functionality btw.
2

You can use str.isdigit, re.search, or the built-in string to number conversions such as float and int:

def extractNumber(string):
    for word in string.split():
        try:
            # The float() function accepts a decimal string.
            # The int() function accepts an integer string.
            return float(word)
        except ValueError:
            pass

number = extractNumber(input())

if number is not None:
    print('Your number is: {}'.format(number))
else:
    print('No number found')

3 Comments

ok i tried it but i only recognises it if there is only an integer in the input. I want it to recognise it when it is in a sentence like "I am 20 years old"
\d+ will match one to many occurrences of digits, then it will detect floats
Yes, you can parse floats with the float function above.
0

the regex you're looking for is: \w*[0-9]+\w*. It accepts anything that contains at least one digit.

\w* is the same as [a-zA-Z0-9_]* meaning it will match any lower/upper -case letter, digit or _ literally. It can occur 0 to many times in the string

[0-9]+ matches one to many digits from 0 to 9.

Actually, if your intention is just to check whether the string has digits you are good to go just with [0-9]+ that's the same of what you posted originally.

You can try and create regexes using this site regex101.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.