首页 > 解决方案 > Python——总是得到“必须是 str 而不是 int”

问题描述

我想循环阅读单词,直到用户键入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 是“它”

回复 5 是“曾经”

期望输出: “已经多久了”

标签: python

解决方案


很难说出您想要什么,但根据您的编辑,您似乎正在尝试执行以下操作:

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)

推荐阅读