我正在开发一个 python 程序。我想接受少于 140 个字符的用户输入。如果句子超过字数限制,它应该只打印 140 个字符。我可以输入字符,但这就是发生的事情。我是 python 新手。我怎样才能做到这一点?
def isAlpha(c):
if( c >= 'A' and c <='Z' or c >= 'a' and c <='z' or c >= '0' and c <='9'):
return True
else:
return False
def main():
userInput = str(input("Enter The Sentense: "))
for i in range(140):
newList = userInput[i]
print(newList)
这是我得到的输出
Enter The Sentense: this is
t
h
i
s
i
s
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
main()
File "C:/Users/Manmohit/Desktop/anonymiser.py", line 11, in main
newList = userInput[i]
IndexError: string index out of range
感谢您的帮助