gpt4 book ai didi

python - 字符串索引超出范围错误

转载 作者:行者123 更新时间:2023-11-30 23:41:09 24 4
gpt4 key购买 nike

嘿,我不断收到字符串索引超出范围的错误...我已经尝试了所有方法,请您帮忙,非常感谢。

x = 0
y = ""
z = 0
user_input = raw_input("Message? ")

while z < len(user_input):
y = y + " " + user_input[x]
x = x +3
z += 1
if x > len(user_input):
print y
break

最佳答案

只有在 if x > len(user_input): 时才会中断。但是如果x % 3 == 0呢?

这意味着输入字符串的长度可以被三整除。每次将 x 增加 3,当输入字符串的长度是 3 的倍数时,您会收到错误。

要修复此问题,您应该使用 if x >= len(user_input): 来代替。

关于python - 字符串索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12235270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com