AttributeError:'str'对象没有属性'toLowerCase'

时间:2013-09-17 16:57:08

标签: python

我正在编写一个程序,要求您输入5个单词(一次一个),然后以相反的顺序打印出来。 (我使用的是Python 3.3.2) 这是它应该是什么样子: http://s11.postimg.org/rayd8m3oj/Untitled.png

但相反它给了我这个:

http://s10.postimg.org/c1p590vex/example.png

这是我的代码:

fifth_word = input("Please enter your 1st word: ")
fifth_word = fifth_word.toLowerCase
fourth_word = input("Please enter your 2nd word: ")
fourth_word = fourth_word.toLowerCase
third_word = input("Please enter your 3rd word: ")
third_word = third_word.toLowerCase
second_word = input("Please enter your 4th word: ")
second_word = second_word.toLowerCase()
first_word = input("Please enter your 5th word: ")
first_word = first_word.capitalize()
print("The sentence is: " + first_word + second_word + third_word + fourth_word + fifth_word)

提前致谢

2 个答案:

答案 0 :(得分:5)

Python str类不包含名为toLowerCase的方法。您正在寻找的方法是lower

当您遇到这样的错误消息时,您应该做的第一件事就是查看相关课程可以做什么。

>>> s = 'some string'
>>> dir(s)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__'
, '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul_
_', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__'
, '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_m
ap', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'ist
itle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition
', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

如您所见,toLowerCase不在此处。但是你也可以看到lower哪个应该引导你朝着正确的方向前进。并且不要害怕查看总是具有卓越品质的文档。

答案 1 :(得分:4)

改为使用str.lower()

fifth_word = input("Please enter your 1st word: ")
fifth_word = fifth_word.lower()
fourth_word = input("Please enter your 2nd word: ")
fourth_word = fourth_word.lower()
third_word = input("Please enter your 3rd word: ")
third_word = third_word.lower()
second_word = input("Please enter your 4th word: ")
second_word = second_word.lower()
first_word = input("Please enter your 5th word: ")
first_word = first_word.capitalize()
print("The sentence is: " + first_word + second_word + third_word + fourth_word + fifth_word)