2012-12-15 4 views
2

이전 루프를 사용하여 이전의 99 병 노래를 수행하고 While 루프를 사용하여 루프 유형을 계속 학습하는 데 도움을줍니다.TypeError : 형식 문자열에 대한 인수가 충분하지 않습니다. While 루프를 사용합니다.

아래 코드를 볼 때 왜 TypeError가 발생하는지 정확히 궁금합니다. '

Traceback (most recent call last): 
    File "beer.py", line 47, in <module> 
      print "%d %s of beer on the wall," % bottles, s1 
TypeError: not enough arguments for format string 

나는 % 후 "병 (S1)"는 주위에 괄호를 사용하여 시도했지만, 여전히 아무튼 : 여기

내 코드입니다 :

# Get number of beers 
bottles = int(raw_input("How many bottles of beer? ")) 

# return invalid response 
if bottles < 1: 
    print "That's not a good number" 

    if bottles == 1: 
     s1 = "bottle" 
     s2 = "bottles" 

    elif bottles == 2: 
     s1 = "bottles" 
     s2 = "bottles" 

# sing verses 
while bottles > 0: 
    print "%d %s of beer on the wall," % bottles, s1 
    print "%d %s of beer." % bottles, s1 
    print "You take one down, pass it around," 
    print "%d %s of beer on the wall." % (bottles - 1), s2 
    print 
    bottles -= 1 

그리고 여기에 오류가 도와 줘. 어떤 제안? `사용

당신이 튜플 여러 인수를 지정해야
+0

(병, S1)'나를 위해 작동합니다. – Tim

+0

병 입력이 10 일 경우 어떻게됩니까?'s1'과's2'는 어디에 정의됩니까? – jdi

+0

'bottles'를 2보다 큰 값으로 설정하면 코드가 NameError를 생성합니다.이 경우, if/elif는 실행되지 않습니다. 엘프를 if로 변경해야합니다. 또한 들여 쓰기 문제가있는 것처럼 보입니다. if/elif는 if 병 <1 '아래에 있으면 안됩니다. – BrenBarn

답변

5

, 예를 들어

print "%d %s of beer on the wall," % (bottles, s1) 
+0

도움을 주셔서 감사합니다. 이러한 인수를 튜플로 지정하는 것과 함께, 나는 while 루프가 잘못된 위치에 있었고, 대소 문자 구별 아래에 있었음에 틀림 없음을 깨달았다. 2.) else 절을 ​​허용하지 않았다. 2보다 높은 숫자. –

관련 문제