저는 Python 초보자이고 YouTube에서 Cory Schafer의 Booleans 및 Conditionals에 대한 자습서를 보았습니다. 그는 Python이 False로 간주하는 값을 보여 주려고 할 때 세그먼트가있었습니다. 그는 하나씩 테스트했지만 더 효율적이고 재미있는 방법이 있는지 궁금해서 for 루프 문을 생각해 보았습니다. 출력이 Evaluated to False 8 줄이 될 것으로 예상하고 있지만 Evaluated to True를 계속 얻습니다. 누군가 나를 깨달을 수 있습니까? 감사합니다!
condition = (False, None, 0, 0.00, '', (), [], {})
for i in condition:
if condition: # It is assumed that condition == true here, right?
print('Evaluated to True')
else:
print('Evaluated to False ')
#OUT:
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
Evaluated to True
변경 if condition
에 if i
. condition
전체 튜플을 8 번 테스트하는 대신 튜플에서 가져온 각 개별 항목을 테스트하려고합니다 .
더 명확한 이름을 지정하면이 문제를 해결할 수 있습니다. 나는 항상 컬렉션 s
에 끝에 가있는 복수의 이름을 부여하는 것이 좋습니다. 그런 다음 더 자연스럽게 읽는 다음을 작성할 수 있습니다.
conditions = (False, None, 0, 0.00, '', (), [], {})
for condition in conditions:
if condition: # It is assumed that condition == true here, right?
print('Evaluated to True')
else:
print('Evaluated to False ')
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다