【发布时间】:2013-12-25 19:25:14
【问题描述】:
这是我的代码:
quote = input("Enter a Sentence: ")
a = len(quote)
counter = 0
counter1 = 0
reverse = a
print("The Length of the sentence is",a,"characters long!")
for x in range(0,a):
if str.isspace(quote[x]) == True:
counter = counter + 1
print("The Length of the sentence is",a - counter,"characters long (excluding space...)!")
for x in range(0,a):
if str.isupper(quote[x]) == True:
counter1 = counter1 + 1
print("The number of Upper Case Characters in the sentence is",counter1,"characters!")
print("The number of Lower Case Characters in the sentence is",a-counter1,"characters long!\n")
while reverse >= 1:
r = reverse
print(quote[r])
r = r - 1
它的目的是找到关于该句子的所有内容,但唯一的问题是底部的“While”循环。它似乎不起作用,其目的是“反转”句子。但是,它给了我一个看起来有点像这样的错误:
Traceback (most recent call last):
File "C:\Documents and Settings\ususer\My Documents\Downloads\StringStuff.py", line 27, in <module>
print(quote[r])
IndexError: string index out of range
我做错了什么?请帮忙!
【问题讨论】:
-
使用
print(r)发现您的问题
标签: python for-loop while-loop syntax-error python-3.3