본문 바로가기
파이썬

파이썬 내 파이썬 if 문이 작동하지 않는 이유는 무엇입니까?

by º기록 2020. 12. 22.
반응형

누군가 나를 도울 수 있기를 바랍니다. 아래 기능을 실행하면 입력 내용에 관계없이 규칙이 인쇄됩니다. 내가 뭘 잘못했는지 알 수 없다.

def check_rules():
    while True:
       request = input("\nWould you like to know the rules? (y/n) ")
       if request == "y" or "Y":
           print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
   number as the number rolled to see how many ladybirds
   there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
   card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
           break
        elif request == "n" or "N":
           break
        else:
           print("\nI'm sorry, I didn't understand that.")

 

해결 방법

 

if 문이 올바르게 구성되지 않았습니다.

def check_rules():
    while True:
       request = input("\nWould you like to know the rules? (y/n) ")
       if request in ["y","Y"]:
           print("""
1. Each player takes it in turn to roll a dice.
2. The player then turns over a card with the same
   number as the number rolled to see how many ladybirds
   there are (0-3).
3. The player keeps the card.
4. If a player rolls a number that is not on an unclaimed
   card, play continues to the next player.
5. Play continues until there are no more cards.
6. The player with the most number of ladybirds wins.""")
           break
        elif request in ["n","N"]:
           break
        else:
           print("\nI'm sorry, I didn't understand that.")

부울 표현식은 if something == x 또는 y 와 같을 수 없습니다. if something == x 또는 something == y 와 같이 명시해야합니다.

 

참조 페이지 https://stackoverflow.com/questions/21790669

 

 

반응형

댓글