dummy
dummy

Reputation: 1731

Review: Built-In Functions

This is a two-parter: first, define a function, distance_from_zero, with one parameter (choose any parameter name you like).

Second, have that function do the following:

Check the type of the input it receives. If the type is int or float, the function should return the absolute value of the function input. If the type is any other type, the function should return "Not an integer or float!"

code:

def distance_from_zero(n):
print type(n)
if type(n) == int or type(n) == float:
    var = abs(n)
    print var
    return n
else:
    print "no!"
    return n

var = input("Enter number:")
print var
distance_from_zero(var)

Upvotes: 1

Views: 768

Answers (4)

user6437730
user6437730

Reputation: 1

def distance_from_zero(n):
    if type(n) is int or type (n) is float:
       return abs(n)
       #return  abs(n)
    else:
        return "Nope"

Upvotes: 0

TerryA
TerryA

Reputation: 59974

A couple of things:

input() in Python 2.7 is equivalent to eval(raw_input()). So if you input "hello", it will raise a NameError (unless there is a variable hello). If you're working with Python 2.7, use raw_input(). However, if you are using python 3, then use input(), because raw_input() does not exist in Python 3 (and input is the exact same as raw_input in 3)

You also returned n and not var, the absolute value.

def distance_from_zero(n):
    try:
        return abs(float(n))
    except ValueError:
        return "That is not an integer or float!"

var = raw_input("Enter number:")
print var
distance_from_zero(var)

Also, for checking types, you should be using isinstance().

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

First, as the others have suggested, use raw_input().

Then, you can try to convert it to an int or float. If both of those fail, you don't have a number. If you want to keep the "original" type, you can use this:

def distance_from_zero(n):
    try:
        n = int(n)
    except ValueError:
        try:
            n = float(n)
        except ValueError:
            print "Not a number!"
            n = float("NaN")
    return abs(n)

If you don't mind if the input 1 is converted to 1.0, then you can simplify the function:

def distance_from_zero(n):
    try:
        return abs(float(n))
    except ValueError:
        print "Not a number!"
        return float("NaN")  # or remove this line to return None

In both cases, you would call the function like this:

var = raw_input("Enter number: ")
print distance_from_zero(var)

Upvotes: 1

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

Here your code has some flaws. When you get a user input through input(), it throws error for any non numeric input. use raw_input(). To check the type of the input received, use literal_eval()

import ast
def distance_from_zero(n):
    try:        
         x = ast.literal_eval(n)
         if isinstance(x, (int,float)):
             var = abs(x)
             print type(var)
             return var
    except:
         print "No!"

Upvotes: 1

Related Questions