我正在开发一个需要用户大量输入的程序,
并且如果他正在写/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)))
我正在开发一个程序,该程序需要用户的许多输入,如果他正在编写/结束,则该程序将结束并使用他的输入打印一个字符串。我还补充说,如果用户的字符串具有...
这里有很多选择。效率最低,但最简单的方法可能是直接附加到字符串:
您可以这样操作: