2017-03-17 4 views
0

Python을 처음 사용합니다. while 루프에서는 사용자에게 입력을 요청합니다.이 입력은 dict의 핵심입니다. 그 키의 값을 인쇄하십시오. 이 프로세스는 입력이 dict의 키와 일치하지 않을 때까지 계속되어야합니다. if 문을 사용하여 키가 사전에 있는지 확인합니다. 그렇지 않다면 깨는 while 루프와 같습니다. 지금까지 나는 그것을 깨뜨릴 수 없다. 파이썬은 사용자 입력으로 While 루프 깨기

는 모든

Animal_list = { 
    'lion': 'carnivora', 'bat': 'mammal', 'anaconda': 'reptile', 
    'salmon': 'fish', 'whale': 'cetaceans', 'spider': 'arachnida', 
    'grasshopper': 'insect', 'aligator': 'reptile', 'rat': 'rodents', 
    'bear': 'mammal', 'frog': 'amphibian', 'turtles': 'testudines' 
} 
while True: 
    choice = raw_input("> ") 
    if choice == choice: 
     print "%s is a %s" % (choice, Animal_list[choice]) 
    elif choice != choice: 
     break 
+0

나는 또한 같은 제안합니다, 파이썬이 시퀀스의 회원을 확인하는 데 사용됩니다 "에서"연산자, 문자열, 튜플 etcetra. 당신은이 링크에서 예를 들어 확인할 수 있습니다 : https://www.tutorialspoint.com/python/membership_operators_example.htm –

답변

0

choice == choice이 항상 true 될 것입니다 감사합니다. 당신이 정말로하고 싶은 것은 choiceAnimal_list에 있는지 확인하는 것입니다. 이로 변경 시도 : @christopher 제안 것처럼

Animal_list = { 
    'lion': 'carnivora', 'bat': 'mammal', 'anaconda': 'reptile', 
    'salmon': 'fish', 'whale': 'cetaceans', 'spider': 'arachnida', 
    'grasshopper': 'insect', 'aligator': 'reptile', 'rat': 'rodents', 
    'bear': 'mammal', 'frog': 'amphibian', 'turtles': 'testudines' 
} 
while True: 
    choice = raw_input("> ") 
    if choice in Animal_list: 
     print "%s is a %s" % (choice, Animal_list[choice]) 
    else: 
     break 
+0

좋아요! 고맙습니다. 나는 항상 파이썬이 영어에 너무 가깝다는 것을 놀랍다. –

+0

나의 기쁨! 여기에 대한 대답이 문제를 해결할 수 있다면 마음에 들면 인정 된 것으로 표시하십시오 :) –