简体   繁体   English

我正在尝试显示使用输入 function 获得的字符串的索引值

[英]I am trying to display the index value of strings which I have obtained using the input function

index=0

while True:
    stri=input("enter the strings : ")
    if stri=="Exit" or stri=="exit":
        break
    for indexes in stri:
        letter=stri[index]
        print(index, letter)
        index=index+1

After displaying the index value of the first string entered it is showing an error显示输入的第一个字符串的索引值后,显示错误

    enter the strings : abcdfg
0 a
1 b
2 c
3 d
4 f
5 g
enter the strings : qwerty
Traceback (most recent call last):
  File "len.py", line 10, in <module>
    letter=stri[index]
IndexError: string index out of range

this is the error I get这是我得到的错误

As you are already looping through the string, you don't need the extra index:由于您已经在遍历字符串,因此不需要额外的索引:

while True:
    stri=input("enter the strings : ")
    if stri=="Exit" or stri=="exit":
        break
    for index, letter in enumerate(stri):
        print(index, letter)

You can try something like this你可以试试这样的

while True:
    stri=input("enter the strings : ")
    if stri.lower() == 'exit':
        break
    for index, value in enumerate(stri):
        print(index, " "+ value)

you don't need to keep the index use the pointer that you are using for writing the string charcters您不需要保留索引使用您用于编写字符串字符的指针

# index=0

while True:
    stri=input("enter the strings : ")
    if stri=="Exit" or stri=="exit":# you can directly use lower()
        break
    for indexes in range(len(stri)):
        letter=stri[indexes]
        print(indexes+1, letter)

Alternate solution替代解决方案

while True:
    stri=input("enter the strings : ")
    if stri=="Exit" or stri=="exit":# you can directly use lower()
        break
    index=0
    for indexes in range(len(stri)):
        letter=stri[index]
        print(index+1, letter)
        index+=1

you need to init the index after each input您需要在每次输入后初始化索引



while True:
    index=0
    stri=input("enter the strings : ")
    if stri=="Exit" or stri=="exit":
        break
    for indexes in stri:
        letter=stri[index]
        print(index, letter)
        index=index+1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 while 循环,我得到前 2 个字符串的输入,然后在 function 中传递下两个字符串。我收到索引错误 - Using while loop,I am getting the input of first 2 strings and then passing the next two strings in function.I am getting index error 我正在尝试使用上层功能进行输入 - I am trying to use the upper function for the input 我正在尝试使用 exec function 修改字典值 - I am trying to modify a dictionary value using the exec function 我有一个输入文件,我正在尝试从中建立字典 - I have an input file that I am trying to build a dictionary from python 基础知识 - 我正在尝试编写一个 function,它将一个字符串(一个月)作为输入并返回该月的天数 - python basics - I am trying to write a function which takes a string(a month) as an input and returns the amount of days in the month 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function 为什么在尝试显示 dataframe 时出现属性错误,其中包含由 UDF function 创建的列? - why do I have attribute error while trying to display dataframe which has column created by UDF function in it? 我正在尝试遍历返回3个元组的元组的函数的返回值 - I am trying to iterate over the return value of a function which returns a tuple of 3 tuples 我正在尝试获取所选列表项的索引值 - I am trying to get the index value of the list item selected 我正在尝试使用导入模块中的 function 更新全局变量。 两个文件都在同一个文件夹中 - I am trying to update a global variable using a function which is in an imported module. Both files are in the same folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM