[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Python quiz

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 22
Thread images: 1

File: 1482103081807.png (374KB, 600x418px) Image search: [Google]
1482103081807.png
374KB, 600x418px
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 do
def 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
>>
>>58126678
def 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.
Thread posts: 22
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.