2

I am writing a code on the subject of engineering which requires the user to input several values which the program will then work with.

At the moment I have the following code:

while True:
    strainx =input("Please enter a value for strain in the x-direction: ")

    num_format = re.compile("^[1-9][0-9]*\.?[0-9]*")
    isnumber = re.match(num_format,strainx)
    if isnumber:

        break

In simple terms I am trying to ask the user to enter a value for strainx which is a number. If the user enters anything other than a number then the question will be repeated until they enter a number. However, by using this method the code does not accept decimals and there will be instances where the user must enter a decimal. Is there any way around this?

3

4 Answers 4

4

Just try casting to a float and catching a ValueError if the user enters something that cannot be cast:

while True:
    strainx = input("Please enter a value for strain in the x-direction: ")  
    try:
       number = float(strainx)
       break # if valid entry we break the loop
    except ValueError:
        # or else we get here, print message and ask again
        print("Invalid entry")

print(number)

casting to float covers both "1" a "1.123" etc..

If you don't want to accept zero you can check after casting is the number is zero, I presume negative numbers are also invalid so we can check if the number is not <= 0.

while True:
    strainx = input("Please enter a value for strain in the x-direction: ")
    try:
        number = float(strainx)
        if number <= 0:
            print("Number must be greater than zero")
            continue  # input was either negative or 0
        break  # if valid entry we break the loop
    except ValueError:
        # or else we get here, print message and ask again
        print("Invalid entry")

print(number)
Sign up to request clarification or add additional context in comments.

4 Comments

This is EAFP; it’s easier to ask for forgiveness than permission. Just assume that it’s a vaid number and catch the case where it is not.
Okay great thanks, that works! I have several of these inputs. For one of them I want to specify that the input cannot equal zero either, is there any way of doing this?
Sure. After trying to cast add an if check to see if number is zero. If so print a message then use continue putting the break outside the if
I've been trying to write this into the program and I really don't understand how. Could you possibly give me some more guidance? Thanks
2

If you are looking for Integer check then try this -

isinstance( variable_name, int )

If it returns True then the variable is number else it's something else.

But if you want to check if the character value is number or not. eg - a = "2" above script will return False. So try this -

try:
    number = float(variable_name)
    print "variable is number"
except ValueError:
    print "Not a number"

1 Comment

Assuming Python 3, the return value of input will always be a string regardless of its content, so the instanceof check will never work there.
0

If you insist of using regex, this pattern appears to work:

"^(\-)?[\d]*(\.[\d]*)?$"

Match optional negative sign, match any number of digits, optional decimal with any number of digits.

Tip: You can use isnumber = bool(re.match(num_format,strainx) or the latter part directly into the if statement.

1 Comment

You don’t need to put single character class escape sequences (\d) into separate character classes ([ ]). And you also don’t need to escape dashes. You could shorten your expression to just ^-?\d*(\.\d*)?$. And if you use re.match, you don’t actually need the caret ^.
0

If you are using Python 2, instead of using regular expressions, you can use Python's in built type checking mechanisms.

Let's loop while strainx is not a number, then check whether the latest input is a number.

is_number = False
while not is_number:
    strainx =input("Please enter a value for strain in the x-direction: ")
    is_number = isinstance(strainx, float) or isinstance(strainx, int)

8 Comments

how would a string ever be an instance of a float or int?
input() in Python 2.X will cast the input to a sensible data type, i.e 'x' would be string, 2 would be int, 2.2 would be float.
How would the user be using a regex on something that is not a string?
Input returns a string in python 3
There's no reason to assume that the programmer has not made an error by expecting to use regex. I previously asked for further clarification in the comments section to see if they were asking the question he intended to.
|

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.