Python 什么是索引器?我一直都明白
这是我的密码:Python 什么是索引器?我一直都明白,python,for-loop,while-loop,syntax-error,python-3.3,Python,For Loop,While Loop,Syntax Error,Python 3.3,这是我的密码: 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 p
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
回溯(最近一次呼叫最后一次):
文件“C:\Documents and Settings\ususer\My Documents\Downloads\StringStuff.py”,第27行,在
打印(引用[r])
索引器错误:字符串索引超出范围
我做错了什么?请帮忙 Python是
0
索引的,因此字符串的第一个字符是str[0]
,最后一个字符是str[len(str)-1]
。因此,由于您以reverse=len(quote)
开头,因此在结尾处执行的是quote[len(quote)]
,即字符串末尾的一个
因此,您可能应该从reverse=a-1
开始,最后的循环应该类似于:
while reverse >= 0:
print(quote[reverse])
reverse = reverse - 1
Python是
0
索引的,因此字符串的第一个字符是str[0]
,最后一个字符是str[len(str)-1]
。因此,由于您以reverse=len(quote)
开头,因此在结尾处执行的是quote[len(quote)]
,即字符串末尾的一个
因此,您可能应该从reverse=a-1
开始,最后的循环应该类似于:
while reverse >= 0:
print(quote[reverse])
reverse = reverse - 1
Python是
0
索引的,因此字符串的第一个字符是str[0]
,最后一个字符是str[len(str)-1]
。因此,由于您以reverse=len(quote)
开头,因此在结尾处执行的是quote[len(quote)]
,即字符串末尾的一个
因此,您可能应该从reverse=a-1
开始,最后的循环应该类似于:
while reverse >= 0:
print(quote[reverse])
reverse = reverse - 1
Python是
0
索引的,因此字符串的第一个字符是str[0]
,最后一个字符是str[len(str)-1]
。因此,由于您以reverse=len(quote)
开头,因此在结尾处执行的是quote[len(quote)]
,即字符串末尾的一个
因此,您可能应该从reverse=a-1
开始,最后的循环应该类似于:
while reverse >= 0:
print(quote[reverse])
reverse = reverse - 1
您遇到了一个常见问题,即Python从0开始索引,但将列表的长度作为从1开始计数的有效整数返回。这导致对于任何列表,
l[len(l)]
将为您提供索引器,因为长度为10的列表将只有索引0…9。
您所要做的就是初始化reverse=len(quote)-1
。
您还必须在while循环中降低循环变量,因此使用
reverse-=1
而不是r=r-1
您遇到了一个常见问题,即Python从0开始索引,但将列表的长度作为从1开始计数的有效整数返回。这导致对于任何列表,l[len(l)]
将为您提供索引器,因为长度为10的列表将只有索引0…9。
您所要做的就是初始化reverse=len(quote)-1
。
您还必须在while循环中降低循环变量,因此使用
reverse-=1
而不是r=r-1
您遇到了一个常见问题,即Python从0开始索引,但将列表的长度作为从1开始计数的有效整数返回。这导致对于任何列表,l[len(l)]
将为您提供索引器,因为长度为10的列表将只有索引0…9。
您所要做的就是初始化reverse=len(quote)-1
。
您还必须在while循环中降低循环变量,因此使用
reverse-=1
而不是r=r-1
您遇到了一个常见问题,即Python从0开始索引,但将列表的长度作为从1开始计数的有效整数返回。这导致对于任何列表,l[len(l)]
将为您提供索引器,因为长度为10的列表将只有索引0…9。
您所要做的就是初始化reverse=len(quote)-1
。
您还必须降低while循环中的循环变量,因此使用
reverse-=1
而不是r=r-1
注意,他的当前while循环将永远不会退出,因为他将r
递减,而不是reverse
。谢谢,但它似乎不起作用。它只打印第二个字符。还要注意,他当前的while循环将永远不会退出,因为他将r
,而不是reverse
。谢谢,但它似乎不起作用。它只打印第二个字符。还要注意,他当前的while循环将永远不会退出,因为他将r
,而不是reverse
。谢谢,但它似乎不起作用。它只打印第二个字符。还要注意,他当前的while循环将永远不会退出,因为他将r
,而不是reverse
。谢谢,但它似乎不起作用。它只打印第二个字符。使用print(r)
查找问题使用print(r)
查找问题使用print(r)
查找问题使用print(r)
查找问题