플레이어가 전구를 가져 오면 인벤토리에 "bulb"가 추가되고 light_bulb 변수가로 설정됩니다 True
. 나는 print(light_bulb)
코드에 추가 했으며 실제로 True
전구를 가져 가면 설정됩니다.
and not light_bulb:
끝에 배치 가 있는데도 9 호선이 달리는 이유를 이해할 수 없습니다 . 로 light_bulb
설정된 대로 코드가 두 번째 또는 세 번째 또는 네 번째로 실행 됩니다 True
. and not True
또는로 평가됩니다 and False
. 이것은 라인 14가 9 대신에 실행되어야 함을 의미합니다.
inventory = ["null"]
def lol():
choice1 = input(">")
light_bulb = False
if "bulb" in inventory:
light_bulb = True
if "bulb" in choice1.lower() or "unscrew" in choice1.lower() or "lightbulb" in choice1.lower() or "light" in choice1.lower() and not light_bulb:
inventory.append("bulb")
print("You try to fix the light bulb but it comes off. You decide to keep it.")
alley()
elif "bulb" in choice1.lower() or "unscrew" in choice1.lower() or "lightbulb" in choice1.lower() and light_bulb:
print ("You already have a light bulb!")
alley()
else:
dead("You fumble around in the darkness and accidentally kill yourself.")
문제는 or
및 and
표현식이 평가 되는 순서 와 관련 이 있습니다. and
운전자는보다 높은 우선 순위 갖는 or
첫 평가 그래서, 선행 표현. 대신 마지막으로 평가하기를 원한다고 생각합니다. 괄호를 추가하여 순서를 명시 적으로 만드십시오 (이렇게하면 줄을 더 합리적인 길이로 줄 바꿈 할 수도 있습니다).
if ("bulb" in choice1.lower() or "unscrew" in choice1.lower() or
"lightbulb" in choice1.lower() or "light" in choice1.lower()) and not light_bulb:
문자열을 확인하는 "lightbulb"
것은 아마도 필요하지 않을 것입니다. 그 이유는 그 부분 문자열 "light"
을 "bulb"
따로 확인하기 때문입니다 ( in
연산자는 단어 경계를 존중하지 않습니다).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다