Query on python code


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

```python

Replace this line with your code.

why “or” cant be work for two arguments, in below code if I re write code for second line ( if type(s) == int or float: ) its not working . can any one explain it please.

def distance_from_zero(s):
if type(s) == int or type(s) == float:
return abs(s)
else :
return “Nope”

distance_from_zero(-2)


#2

Your double-quotes " are a problem here as we use UTF-8

def distance_from_zero(s):
    #remember indentation is IMPORTANT
    #4 spaces perlevel
    if type(s) == int or type(s) == float:
        return abs(s)
    else :
        return 'Nope'

print distance_from_zero('2')
print distance_from_zero(-2)

#
#==== or the python way
#
def distance_from_zero(s):
    if isinstance(s, (int, float)) :
        return abs(s)
    else :
        return 'Nope'

print distance_from_zero('2')
print distance_from_zero(-2)


#3

def distance_from_zero(s):
#remember indentation is IMPORTANT
if type(s) == (int or float): # This is how it is done
return abs(s)
else :
return “Nope”

print distance_from_zero(‘2’)
print distance_from_zero(-2)

here


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.