[英]Why does not the variable in the while loop, change the boolean from True to False?
為什么不在while循環中檢查變量,將boolean從True改為False?
def word_valid():
words = input("Your word: ").upper()
global check
check = False
return words
def main():
check = True
while check is True:
words = word_valid()
print(words) #Won't print out
if __name__ == '__main__':
main()
global
指的是全局 scope,但main
中的check
是main
的 scope 的本地。 也就是說,您在word_valid
中更改的check
與main
中的check
不同
無需過多考慮如何很好地做到這一點,您可以返回check
def word_valid():
words = input("Your word: ").upper()
check = False
return words, check
def main():
check = True
while check is True:
words, check = word_valid()
print(words) #Won't print out
if __name__ == '__main__':
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.