0

Hi I am curious if there is a way to convert user input to an specific variable type based off the content of the input (I'm using raw_input(), not sure if there's a better function for this). I know raw_input() always makes a string, but can said string be converted to an integer or a floating point if the input is valid? For example, if the user enters '1,' is there a way to have that variable stored as an integer rather than a string? Here is the code I am attempting to make work:

print "Subtraction Calculator (x-y)"

def calculator(x, y):
  if [isinstance(x, int) or isinstance(x, float)] and [isinstance(y, int) or 
isinstance(y, float)]: #This should be checking to see if x and y are both
numerical
  D = x - y 
  return D
 else:
  return "One or more inputs is not a number."

x = raw_input("x = ")
y = raw_input("y = ")

print calculator(x, y)

Obviously this code doesn't work as x and y are both strings due to raw_input(), but I don't seem to be getting the proper error message ("One or more inputs is not a number.") and am instead getting an error at (D = x - y). I believe that this is due to my 'if' statement always registering as True, but I am not sure why this is either.

2
  • 1
    you can change string to int by use of int() method like x= int(raw_input("x = ")) Commented Dec 22, 2017 at 19:16
  • 1
    [isinstance(x, int) or isinstance(x, float)] is a non-empty list and thus true. Commented Dec 22, 2017 at 19:25

2 Answers 2

1

You can do this using exception handling. Basically, the idea is simply try to convert x and y into floats and do the calculation you want. If x or y are not able to be converted to floats, python will raise a ValueError which you can catch in a try and except statement. You could make a loop so that you keep asking the user to input x and y until it works like so:

    while(True):
        try:
            x = raw_input('x = ')
            y = raw_input('y = ')
            print(float(x)-float(y))
            break
        except ValueError:
            print('One of those was not a float! Try again!')
Sign up to request clarification or add additional context in comments.

Comments

0

What about simply returning the result that you want and if one of the two cannot be converted to a number, it will raise a ValueError:

def calculator(x, y):
    return float(x) - float(y)

x = raw_input("x = ")
y = raw_input("y = ")

print calculator(x, y)

Note that if you do want to check if a variable is either an int or float you can use isinstance(x, (int, float)). Therefore, your code can be rewritten as:

def calculator(x, y):
    if isinstance(x, (int, float)) and isinstance(y, (int, float)):
        return x - y
    return "One or more inputs is not a number."

Comments

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.