尝试查找字符串大小时出错

时间:2016-10-27 19:24:36

标签: python

我试着看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 '!@#$%^&*()_+{}|:"<>?':
        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 '!@#$%^&*()_+{}|:"<>?':
        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 '!@#$%^&*()_+{}|:"<>?':
        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部分的效果很好。

1 个答案:

答案 0 :(得分:0)

len功能将为您提供完美的服务; len(word)将为您提供字符串的字符数。保存到文件时所需的字节大小取决于编码。

顺便说一下 - 为了避免混淆:

  • 不要为变量word命名,您希望它包含多个单词。我建议像text
  • 不要使用内置函数的名称作为变量(str)。它有助于使用具有Python语法高亮功能的编辑器。