如何将程序的输入保存到程序的文件中。仅限Python:我需要将其添加到此:
sentence = input("Please input a sentence: ")
print(sentence)
word=input("Please enter word and position wil be shown: ")
if word in sentence:
print("Found word")
else:
print ("Word not found")
但是我没有头绪
您的问题似乎包括两个主要部分:如何在Python中获取输入以及如何在Python中将数据保存到文件中。
要从终端获取输入:
>>> data = input('Input: ')
>>> Input: hello, world!
要保存到文件:
>>> with open('output.txt', 'w') as f:
>>> f.write(data)
编辑0:@Gazzer,如果要保存sentence + input
,则需要做f.write(sentence + input)
而不是使用.save()
。
编辑1:@Gazzer,我得到了类似下面的效果(注意:代码未显示找到的单词的位置):
sentence = input("Please input a sentence: ")
word = input("Please enter word and position wil be shown: ")
with open('output.txt', 'w') as f:
f.write('{} {}'.format(sentence, word))
如果您遇到更多问题,那么网络上就会有数百种资源寻求帮助。堆栈溢出,learnprogramming等。
下次您问一个问题时,如果您提供您正在处理的代码片段以及所遇到的问题/错误,那么对于回答这些问题确实很有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句