2016-10-27 3 views
1

나는 this을 보았지만 아무런 도움이되지 않았습니다.문자열의 크기를 찾으려고 할 때 오류가 발생했습니다

game = 1 
while game == 1: 
    print('WORLD EVALUATOR 2000') 
    word = input("Insert some text...").lower() 

    words = word.split(" ") 
    lastWord = words[-1] 



    if word[0].lower() in 'aeiou': 
     print("The string begins with a vowel") 
    elif word[0].lower() in '1234567890': 
     print ("The string begins with a number") 
    elif word[0].lower() in 'bcdfghjklmnpqrstvwxyz': 
     print ("The string begins with a consonant") 
    elif word[0].lower() in '[email protected]#$%^&*()_+{}|:"<>?': 
     print ("The string begins with a symbol or a space.") 
    elif word[0].lower() in ' ': 
     print ("The string begins with a space.") 

    else: 
     print("The string does not begin with a vowel,number, consonant, space, or a symbol") 

    if word[1].lower() in 'aeiou': 
     print("The string's second letter is a vowel") 
    elif word[1].lower() in '1234567890': 
     print ("The string's second letter is a number") 
    elif word[1].lower() in 'bcdfghjklmnpqrstvwxyz': 
     print ("The string's second letter is a consonant") 
    elif word[1].lower() in '[email protected]#$%^&*()_+{}|:"<>?': 
     print ("The string's second letter is a symbol or a space.") 
    elif word[1].lower() in ' ': 
     print ("The string's second letter is a space.") 
    else: 
     print("The string's second letter is not a vowel,number, consonant, space, or a symbol") 

    if lastWord[-1].lower() in 'aeiou': 
     print("The string's last letter is a vowel") 
    elif lastWord[-1].lower() in '1234567890': 
     print("The string's last letter is a number") 
    elif lastWord[-1].lower() in 'bcdfghjklmnpqrstvwxyz': 
     print("The string's last letter is a consonant") 
    elif lastWord[-1].lower() in '[email protected]#$%^&*()_+{}|:"<>?': 
     print("The string's last letter is a symbol or a space.") 
    elif lastWord[-1].lower() in ' ': 
     print("The string's last letter is a space.") 
    else: 
     print("The string's last letter is not a vowel,number, consonant, or a symbol") 
    print("The string is " + str(len(words)) + ' letters/numbers/symbols long.') 

끝 부분에는 len을 사용하지만 작동하지 않는 부분이 있습니다. 도움이 될만한가요?

누군가가 묻는다면, stackoverflow의 형식이 나를 미워하기 때문에 while 루프가 적절히 들여 쓰기되지 않습니다. 실제로 루프는 len 부분에 대해 잘 작동합니다.

+0

내가 그냥 "1" – CatsInSpace

+0

세계 평가자 2000 일부 텍스트를 삽입 말한다 출력을 얻을 ... Absosc 는 문자열 문자열의 두 번째 문자가 문자열의 마지막 문자가있는 자음 이다 모음으로 시작 자음 문자열은 1 글자/숫자/기호입니다. – CatsInSpace

+4

당신은 문자가 아닌 단어의 수를 세고있다 –

답변

0

len 기능이 완벽하게 작동합니다. len(word)은 문자열의 문자 수를 알려줍니다. 파일에 저장 될 때 필요한 바이트 크기는 인코딩에 따라 다릅니다.

  • 당신이 여러 단어를 포함 할 것으로 예상 변수 word 이름을하지 마십시오 - 그런데

    혼란을 방지 할 수 있습니다. text과 같은 것을 제안합니다.
  • 변수에 내장 함수 이름 (str)을 사용하지 마십시오. 파이썬 구문 강조 기능을 갖춘 편집기를 사용하면 도움이됩니다.
관련 문제