Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.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
Python--始终获取“必须是str而不是int”_Python - Fatal编程技术网

Python--始终获取“必须是str而不是int”

Python--始终获取“必须是str而不是int”,python,Python,我希望在循环中读取单词,直到用户键入stop或。当循环停止时,我想连接并打印用户以前键入的所有单词,不包括stop或space while True: reply = input('Enter text, [type "stop" to quit]: ') print (reply) if reply == 'stop': break while len(reply) >3: reply=reply+1

我希望在循环中读取单词,直到用户键入stop或。当循环停止时,我想连接并打印用户以前键入的所有单词,不包括stop或space

while True:
    reply = input('Enter text, [type "stop" to quit]: ')
    print (reply)
    if reply == 'stop':
        break
    while len(reply) >3:
        reply=reply+1
        print (reply)
例如:

回答1是如何

答复2很长

答复3是:

答复4 isit

答复5已收到

预期产出:有多长时间了


问题不清楚,但我想你说的是,你想迭代一个数字,并将其修改为循环,循环。如果这是真的,下面的内容可能会有所帮助

while True:
    reply = input('Enter text, [type "stop" to quit]: ')

    print (reply)

    if reply == 'stop':
        break

    replyInt = int(reply)

    while replyInt < 100:
        replyInt = replyInt+1
        print (str(replyInt))
输出:

Enter text, [type "stop" to quit]: how
Enter text, [type "stop" to quit]: long
Enter text, [type "stop" to quit]: has
Enter text, [type "stop" to quit]: it
Enter text, [type "stop" to quit]: been
Enter text, [type "stop" to quit]: stop
 how long has it been

很难说出您想要什么,但根据您的编辑,您似乎正在尝试这样做:

replys = []
while True:
    reply = input('Enter text, [type "stop" to quit]: ')
    print(reply)
    if reply == 'stop' or reply == ' ':
        print(" ".join(replys))
        break
    replys.append(reply)

你想用回复+1做什么??不能向字符串中添加int,如果添加了string,那么这将是一个无限循环输入应该是什么?您试图将1添加到其中,“foo”+1没有任何意义,python不允许这样做。另外,添加reply+reply只会使字符串变长,添加它永远不会使其长度变短,因此循环将永远继续。不清楚您希望程序对给定的输入做什么abcdef您希望得到什么输出?想想看。当lenreply>3时:如果用户键入boop会发生什么?lenreply已经>3,您的循环将永远不会停止以返回到获取更多输入。学会理解你正在编写的代码的实际用途,而不是仅仅敲打键盘;答复2很长;答复3:ishas;答复4 isit;答复5已收到。连接单词。它返回->ValueError:以10为基数的int的无效文本:“how”OK。读了上面的评论,我感到困惑,无法解释你的问题。请说明预期的输入和预期的输出。预期的输出是它有多长时间been@YeiBi你告诉我们每个阶段的输入,而不是每个阶段的输出,你只是告诉我们最终的输出。您只想在用户键入停止后或在给定字数后输出吗?@Jared Correct!这就是我想要的输出。谢谢。谢谢你的说明。
replys = []
while True:
    reply = input('Enter text, [type "stop" to quit]: ')
    print(reply)
    if reply == 'stop' or reply == ' ':
        print(" ".join(replys))
        break
    replys.append(reply)