Python Curiosity

Marcus Burkhart

I am curious as to how this program reads. You have the foo functions up top. "Thing" is a global variable. It sets "Thing" which equals 100 to "Item" which also now equals 100. Return "Item" == 0 is what I am not too sure about. "Item" then is equal to 0? Then going down to the flag = False. So "when not flag:" is called, it asks for users input and for whatever reason until you enter "0" the flag turns True and "Thing" now equals 0. If someone can explain how this works I would appreciate it

def foo(item):
    global thing
    thing = item
    return item == 0

thing = 100
flag = False
while not flag:
    flag = foo(int(input("Give me what I want here: ")))
print(thing)
Martijn Pieters

item is set to whatever the user responded with when prompted for Give me what I want here, converted to an integer. In turn thing is set to that value too (thing = item binds the global thing to whatever item points at).

So if the user enters 0, item is set to 0, then thing is set to 0 and item == 0 returns True, ending the while loop.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사