Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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
TypeError:rstrip arg必须为None或str:Python 3_Python_Python 3.x - Fatal编程技术网

TypeError:rstrip arg必须为None或str:Python 3

TypeError:rstrip arg必须为None或str:Python 3,python,python-3.x,Python,Python 3.x,当我运行我的代码时,我得到了这个 TypeError:rstrip参数必须为None或str 这是我的密码: words = input("Words: ") wordcount = len(words) tweet = str(words) x = 0 while wordcount <= 140: phrase = input("Words: ") tweet = tweet+' '+str(phrase) wordcount = len(tweet)

当我运行我的代码时,我得到了这个

TypeError:rstrip参数必须为None或str

这是我的密码:

words = input("Words: ")
wordcount = len(words)
tweet = str(words)
x = 0
while wordcount <= 140:
    phrase = input("Words: ")
    tweet = tweet+' '+str(phrase)
    wordcount = len(tweet)
    print(wordcount)
    print(tweet)
    x = len(phrase)+1
    if wordcount >= 140:
        break

tweet = tweet.rstrip(x+1)
wordcount = len(tweet)
print(wordcount)
print(tweet)
注意:这是在Python3中

有什么帮助吗?

如果要向rstrip传递一个整数,则必须不传递任何字符或某些字符,如果要从字符串的右侧删除该字符

例如:

'   aaab      '.rstrip()
#'   aaab'

'   aaabcccccc'.rstrip('c')
#'   aaab'

从你的评论来看,听起来你想去掉推特的长度x+1。为此,可以使用字符串切片。My_字符串[a:b]将为您提供一个新字符串,其中包含从字符号a到字符号b-1的字符。如果任一侧为空,它将转到字符串的那一端,即My_字符串[a:]将从a转到字符串的末尾。要从字符串末尾开始,请使用负数。您的代码应该如下所示:

tweet = tweet[:-x]

你认为rstrip能做什么?你希望tweet.rstripx+1能做什么?或者更切题;你认为x+1是无还是字符串?好吧,如果x是lentweet,那么它不应该去掉tweet上x+1的长度吗?你确定不只是想要:tweet=tweet[:140]。rstrip以确保它只有140个字符或更少?切片而不是剥离减去任何尾随空格。。。