Why does this work:
def distance_from_zero(x):
if type(x) == int or type(x) == float:
return abs(x)
else:
return "Nope"
But this does not:
def distance_from_zero(x):
if type(x) == (int or float):
return abs(x)
else:
return "Nope"
I'm not a python expert, but I'd guess (int or float) returns a boolean value.
>>58126082
parens evaluate first you dumb cuck
>>> (int or float)
<type 'int'>
>>> type(2.0) == (int or float)
False
>>> type(2) == (int or float)
True
>>58126082
Or operator works only between bool values.
Int and float are not bool values
type(x) == int is a bool value because of the == operator
>>58126082
Probably cause the expression int or float is an error. You're not saying if the type of x is an int or float, you're saying if the type of x is equal to the boolean value of the expression int or float (that is impossible for python to return, most likely)
>>58126222>>> (float or int)
<type 'float'>
I guess that since both values are not False, the first is returned thanks to short-circuit evaluation
>>58126239
>>58126226
>>58126222
>>58126155
>>58126302
Congratulations, you have made it to level 2:
Explain why variation 1 and 2 works, but variation 3 fails:
var 1:
def bigger(first, second):
print(max(first, second))
return True
bigger(21, 422)
var 2:
def bigger(first, second):
print(first, second)
return True
bigger(21, 422)
var 3:
def bigger(first, second):
print(max(bigger))
return True
bigger(21, 422)
>>58126186
You have failed. As a consolation price, you may take a free windows 10 disk and a subscription to the daily gaymer.
>>58126302>>> ("test" or 2.3)
'test'
>>> (2.3 or "test")
2.3
p. cool
>>58126302
/thread
>>58126341
i don't know how python handles recursion but that function call is totally missing its arguments
>>58126082
>>58126341
How about you learn to use code tags you mongoloid newfag?
>>58126341
I've never programmed in Python or seen any Python tutorial, so I'll guess:
The third one fails because the max function doesn't want another function to be sent as a parameter, especially a function that returns a boolean value.
>>58126082
you could dodef distance_from_zero(x):
if type(x) in (int, float):
return abs(x)
else:
return "Nope"
>>58126341
>>58126082
None of your stupid code works, at all, because python is a stupid language with forced whitespace delineation, and your code has no tabs. Come back with questions from a real language like C/C++.
>>58126640
>>58126408
>>58126405
>>58126390
>>58126364
>>58126358
Thanks lads, last question:
How can I use less return words in the following script:
def rental_car_cost(days):
if days >= 7:
return (days * 40) - 50
elif days >= 3:
return (days * 40) - 20
else:
return days * 40
>>58126678
Use code tags you fucking retard
>>58126678def rental_car_cost(days):
cost = days * 40
if days >= 7:
cost -= 50
elif days >= 3:
cost -= 20
return cost
>>58126728
That's beautiful. But why did I have to keep using return the days in my previous script while you don't have to?
>>58126699
Teach me how to fish senpai, and I will use codetags forever.
>>58126082
Format your code you idiot... read the sticky and use [ c o d e ] [ / c o d e ]
>>58126678
Looks like someone has been using codeacademy
>>58126818
yes, I am also using other sources.
Liking it so far, I am officially a senior python programmer now but the slightest clue how to actually code I have not.
Basically, my slick talk got ahead of my own skill. The salary is top notch though.