3

I am trying to validate a user's input. I only want the user to be able to enter a positive or negative integer. All other inputs (i.e. anything with letters) should be rejected

I have the code below at the minute, however I am getting an error. I'm assuming it has to do with the data types but am unsure how to fix this.

import re

number =input("Please enter a number: ")
number=int(number)
while not re.match("^[0-9 \-]+$", number):
    print ("Error! Make sure you only use numbers")
    number = input("Please enter a number: ")
print("You picked number "+ number)
3
  • You should not cast it to a number I believe. Do you get TypeError: expected string or buffer? Commented Dec 1, 2015 at 15:44
  • Yes, I was thinking along these lines. I just assumed as it is number, I would need to use int? Commented Dec 1, 2015 at 15:48
  • Regular expressions only search for matches in strings. Commented Dec 1, 2015 at 15:49

4 Answers 4

6

If all you care about is that the input was a valid numeric literal, don't even bother with the regexp. int will correctly parse the string or raise an exception.

while True:
    s = input("Please enter a number: ")
    try:
        n = int(s)
        break
    except ValueError:
        print("Error! Make sure you only use numbers")
print("You picked number " + n)
Sign up to request clarification or add additional context in comments.

1 Comment

That is what is called proactive thinking :)
1

Regular expressions need strings as input, not numbers. Thus, you do not have to cast the string to number, and you can omit number=int(number).

Here is a working demo:

number = "2"
if not re.match("^[0-9 -]+$", number):
    print ("Error! Make sure you only use numbers")
print("You picked number "+ number)

Comments

0
import re

while  True:
    number =input("Please enter a number: ")
       # number=int(number) -- This raise an error, for regexp you need use "str"

    #while not re.match("^[0-9 \-]+$", number):
    # in [0-9 \-]  after 9 you use whitespace
    # Also in [0-9 \-]  if minus at the and, not need use backslash

if  re.match("-?\d+$", number) : # minus may be only in first position
    break

print ("Error! Make sure you only use numbers")
    # deleted     number = input("Please enter a number: ")

print("You picked number "+ number)

Comments

0
answer = input(" ")#asks the user for an answer
ansnum = re.match("[0-9 \-]",answer)#checks to see if answer only contains numbers
while not ansnum: #whilst the answer does not contain numbers do....
   answer = input("Enter numbers as your answer: ")#asks the user for a valid answer
   ansnum = re.match("[0-9 \-]",answer)#checks to see if the answer contains numbers

1 Comment

Welcome to SO. While code only answers are answers, please consider improving your post by adding context.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.