[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 15 KB, 618x407, 618px-JeanLucPicardFacepalm.jpg [View same] [iqdb] [saucenao] [google]
4870174 No.4870174 [Reply] [Original]

Computer failure question (total noob):

def visits(name, n):

return "%s visited you %d time%s" % (name, n, [' ','s'][n>1])

print( visits("Harry", 1))
print( visits("Lorie", 3))


Where in here does the time%s verify if n>1 is true. In other words, why would it EVER add an 's' to 'time'.

Or...what's the relationship between n, [' ','s'] and [n>1]. ? I just don't see where the code is actually verifying for n>1 and applying it to 'n'. The terms between the square brackets don't seem to me to have any relationship to one another, therefore why is the 's' selected over the blank when n>1 ?

/fail@being clear.
/fail@everything, actually.

>> No.4870179

It's a test to make sure the time is not negative. In the event n is less than zero it will act is if n is zero. The argument n>1 evaluates to zero if n is equal to zero so its sort of redundant in that case, but it's a quick little way to perform the logic.

>> No.4870193

Yeah, but at what point does it verify whether 'n' is higher than 1 (2+) in order to add an 's' to 'time' ?

I don't understand that; I mean, the way I see it, n>1 is just trying to see if the 's' must be added, but I still don't see WHERE in this code the command is to add the 's'.

At what point does the code say "Alright, n>1, so we're taking the post-comma part of the first square brackets and add it to 'n' ?

Why doesn't it just say "Oh, hey, n>1." and not do anything ? What makes it so it actually goes to retrieve the 's' ?