2016-10-27 45 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的地方,但它不工作,我能有一些幫助嗎?

如果有人問,while循環沒有正確縮進,因爲在stackoverflow中的格式不喜歡我。實際上,循環對len部分的工作很好。

+0

我得到的輸出,只是說,這是「1」 – CatsInSpace

+0

WORLD EVALUATOR 2000 插入一些文字... Absosc 的字符串開頭元音 字符串的第二個字母是輔音 字符串的最後一個字母是輔音 該字符串是1個字母/數字/符號長。 – CatsInSpace

+4

你正在計算的字數不是字母 –

回答

0

len函數將爲您完美服務; len(word)會給你你的字符串的字符數。保存到文件時需要的字節大小取決於編碼。

順便說一句 - 以避免混淆:

  • 不要將其命名變量word您希望包含幾個單詞。我會建議像text
  • 不要使用變量的內置函數的名稱(str)。它有助於使用爲Python提供語法高亮功能的編輯器。