Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取索引器:python中的字符串索引超出范围_Python_Python 3.x_String - Fatal编程技术网

获取索引器:python中的字符串索引超出范围

获取索引器:python中的字符串索引超出范围,python,python-3.x,string,Python,Python 3.x,String,获取IndexError:string索引超出python的范围,我是python的初学者,正在尝试制作一个计算大写和小写字母总数的程序,我还尝试了一些解决方案,例如将字符串的空格作为string.replace删除,但再次出现错误 我的代码 尝试使用lenstring而不是25,因为在这里您将遍历所有25个整数。但在您的情况下,字符串中必须有25个字符不是强制性的,可以是25个或少于25个。因此,当它少于25个字符,并且您尝试使用25个字符时,它将显示错误: 而不是: while i <

获取IndexError:string索引超出python的范围,我是python的初学者,正在尝试制作一个计算大写和小写字母总数的程序,我还尝试了一些解决方案,例如将字符串的空格作为string.replace删除,但再次出现错误

我的代码

尝试使用lenstring而不是25,因为在这里您将遍历所有25个整数。但在您的情况下,字符串中必须有25个字符不是强制性的,可以是25个或少于25个。因此,当它少于25个字符,并且您尝试使用25个字符时,它将显示错误:

而不是:

while i < 25 :
尝试:

while i < len(string)
尝试使用lenstring而不是25,因为在这里您将遍历所有25个整数。但在您的情况下,字符串中必须有25个字符不是强制性的,可以是25个或少于25个。因此,当它少于25个字符,并且您尝试使用25个字符时,它将显示错误:

而不是:

while i < 25 :
尝试:

while i < len(string)
您也可以尝试以下方法:

string = input('Enter string upto 25 words: ')
if len(string) > 25:
    print('Please enter a string less than 25 words.')
else:
    total_lwr = sum(map(str.islower, string))
    total_upr = sum(map(str.isupper, string))
你不需要使用while,也不需要更换任何东西。更少的代码:

您也可以尝试以下方法:

string = input('Enter string upto 25 words: ')
if len(string) > 25:
    print('Please enter a string less than 25 words.')
else:
    total_lwr = sum(map(str.islower, string))
    total_upr = sum(map(str.isupper, string))
你不需要使用while,也不需要更换任何东西。更少的代码:

时间应如下所示:

while i < len(string):
守则:

string = input('Enter string upto 25 words: ')
if len(string) > 25:
  print('Please enter a string less than 25 words.')
else:
  string.replace(" ", "")
  total_upr = 0
  total_lwr = 0
  i = 0
  while i < len(string):
    print(i)
    if string[i].islower():
      total_lwr += 1
    elif string[i].isupper():
      total_upr += 1
    i += 1
  print(f"'{string}' contains {total_lwr} lower-case letters and {total_upr} upper-case letters.")
while应如下所示:

while i < len(string):
守则:

string = input('Enter string upto 25 words: ')
if len(string) > 25:
  print('Please enter a string less than 25 words.')
else:
  string.replace(" ", "")
  total_upr = 0
  total_lwr = 0
  i = 0
  while i < len(string):
    print(i)
    if string[i].islower():
      total_lwr += 1
    elif string[i].isupper():
      total_upr += 1
    i += 1
  print(f"'{string}' contains {total_lwr} lower-case letters and {total_upr} upper-case letters.")

谢谢兄弟,但我们的老师没有教我们for loop,我们只学习while loop,因为for loop是用于可迭代对象的,所以先生说会在列表后学习,你能用while制作吗?是的,我已经解决了,@VikasDamodar edit it我会接受it@VikasDamodarr你在吗?谢谢兄弟,但我们的老师并没有教我们for loop,我们只学了while loop,因为for loop是用于可迭代对象的,所以先生说我们会在列表后学习,你们能用while来做吗?是的,我已经解决了,@VikasDamodar edit it我会接受的it@VikasDamodarr你在这儿吗?