使用多个用户输入创建一个字符串

问题描述 投票:0回答:1

我正在开发一个需要用户大量输入的程序,

并且如果他正在写/end,程序将结束并使用他的输入打印一个字符串。我还补充道,如果用户的字符串以疑问词开头,它将自动在字符串中添加问号。

现在,我的问题是我不知道如何将所有字符串放在一起,我只用append尝试过,但它不起作用。

程序:

def stringCreator(mySTR):
    capitalized = mySTR.capitalize()
    questions = ('what', 'how', 'can', 'why')
    if mySTR.startswith(questions):
        return '{}?'.format(capitalized)
    else:
        return '{}.'.format(capitalized)


result = # What should I use there ?
while True:
    userInput = input('Say something: ')
    if userInput == '/end':
        break
    else:
        # Connect all string together with result


print(' '.join(stringCreator(result)))

编辑:

Wolph的帮助下,我得到了这个结果。

[首先,我用result变量创建了一个列表,其次,我已将else条件添加到result.append(userInput)

然后我从[]更改了print功能>

[print(' '.join(stringCreator(result)))print(stringCreator(''.join(result)))

最终程序:

def stringCreator(mySTR):
    capitalized = mySTR.capitalize()
    questions = ('what', 'how', 'can', 'why')
    if mySTR.startswith(questions):
        return '{}?'.format(capitalized)
    else:
        return '{}.'.format(capitalized)


result = []
while True:
    userInput = input('Say something: ')
    if userInput == '/end':
        break
    else:
        result.append(userInput)


print(stringCreator(' '.join(result)))

我正在开发一个程序,该程序需要用户的许多输入,如果他正在编写/结束,则该程序将结束并使用他的输入打印一个字符串。我还补充说,如果用户的字符串具有...

python
1个回答
3
投票

这里有很多选择。效率最低,但最简单的方法可能是直接附加到字符串:


3
投票

您可以这样操作:

© www.soinside.com 2019 - 2024. All rights reserved.