6

我正在编写一个程序,要求您输入 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)

提前致谢

4

2 回答 2

11

Pythonstr类不包含名为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哪个应该引导您朝着正确的方向前进。并且不要害怕查看质量始终如一的文档。

于 2013-09-17T17:02:09.537 回答
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)
于 2013-09-17T16:58:45.730 回答