繁体   English   中英

我无法处理Python中的“ IndexError:字符串索引超出范围”

[英]I can't deal with “IndexError: string index out of range” in Python

def words():
quote = input("Enter a 1 sentence quote, non-alpha separate words: ")
word = ""
for ltr in quote:
    if ltr.isalpha():
        word = word + ltr
    else:
        if word[0].lower() > "g":
            print(word.upper())
            word = ""
        else:
            word = ""

这里的所有信息

说真的,我不知道该怎么办。

不要使用word[0]因为word可能仍然为空,但仍未通过isalpha检查。 首先检查len(word)

if len(word) and word[0].lower() > 'g':

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM