2014-02-10 3 views
0

일부 학생에게 간단한 퀴즈를 만들려고 노력하고 있는데, 잘못된 문자를 입력하면 반복되는 질문을하고 싶지만, 어떻게해야하는지 잘 모르겠습니다. 누군가 도울 수 있습니까?조건부 파이썬을 사용한 원시 입력

player score=[] 

x2=raw_input("Question 3" '\n' 

"How many tests have you taken in past 24 hours?" '\n' 

"A) 0" '\n' 

"B) 1" '\n' 

"C) 2" '\n' 

"D) 3" '\n' 

"E) Too many to count"'\n') 



if x2=="A": 

    player_score.append('0') 

elif x2=="B": 

    player_score.append('1') 

elif x2=="C": 

    player_score.append('2') 

elif x2=="D": 

    player_score.append('3') 

elif x2=="E": 

    player_score.append('4') 

else: 

    print "you typed the wrong letter" 

print player_score 
+0

먼저 '숙제에 대한 도움'이라고 표시 하시겠습니까? 둘째, 루프하려는 경우 루프를 추가해야합니다. – user590028

+1

@ user590028 : 더 이상 숙제 상태로 질문에 태그를 추가하지 않습니다 ([여기] (http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-official-deprecated) 참조). – DSM

+0

왜 player_score는 문자열/정수가 아닌 목록입니까? –

답변

1

일반적으로 올바른 입력을 보장하고 입력이 부적절한 경우 다시 프롬프트하는 가장 좋은 방법은 루프를 사용하는 것입니다. 이 경우 while 루프를 실행 해 봅시다.

player_score = [] 

answer = '' 

while answer.casefold() not in ('a','b','c','d','e'): 
    answer = raw_input("Question 3" "\n" 
    # etc etc etc 

즉, 당신이 퀴즈를 만드는 것처럼 보였으므로 이에 대해 더 좋은 방법이있을 수 있습니다. 나는

questions = [ 
"""Question 3 
How many tests have you taken in past 24 hours? 
A) 0 
B) 1 
C) 2 
D) 3 
E) Too many to count""", 

"""Question 4 
What kind of question should you write here? 
A) I don't know 
B) No bloomin' idea 
C) It escapes me 
D) Foobar 
E) One with a question mark?"""] 

player_score = [] 
for question in questions: 
    answer = '' 
    while answer.casefold() not in ('a','b','c','d','e'): 
     answer = raw_input(question+"\n>>") 
     answer = answer.casefold() 
     if answer == 'a': player_score.append(0) 
     if answer == 'b': player_score.append(1) 
     if answer == 'c': player_score.append(2) 
     if answer == 'd': player_score.append(3) 
     if answer == 'e': player_score.append(4) 
     else: print("Invalid response") 
+0

'None'은 언제'casefold' 메서드가 있습니까? ; ^) – DSM

+0

@DSM Python4.11에서 추가했습니다. 슈퍼 초기 개발 버전 (고정식) –

+0

문자열에 casefold 메소드가 있습니까? (파이썬 2.7 ...이 아님) : s –

0

할 수있는 간단한 일이 루프에 넣고있다 .... 모든 질문 ("A" == 0, "B" == 1, "C" == 2, "D" == 3, "E"==4)에 대한 동일 그래서 대신에이 작업을 수행하게됩니다 답변을 믿고있어 수용 가능한 입력을 할 때까지 계속 노력하십시오.

#... as before, then... 
done = False 

while done == False: 

    x2=raw_input("Question 3" '\n' 
     "How many tests have you taken in past 24 hours?" '\n' 
     "A) 0" '\n' 
     "B) 1" '\n' 
     "C) 2" '\n' 
     "D) 3" '\n' 
     "E) Too many to count"'\n') 


    if x2=="A": 
     player_score.append('0') 
     done = True 
    elif x2=="B": 
     player_score.append('1') 
     done = True 
    elif x2=="C": 
     player_score.append('2') 
     done = True 
    elif x2=="D": 
     player_score.append('3') 
     done = True 
    elif x2=="E": 
     player_score.append('4') 
     done = True 
    else: 
     print "you typed the wrong letter" 

반복 및 부울은 약간 불쾌하므로 리팩터링 될 수 있습니다.

+0

while 루프의 시작 부분에서'done = True'를 설정 한 다음 else 블록에서'done = False'를 설정할 수 있습니다. –