我创建了一个代码,要求用户在纯文本文件中输入位置列表,将用户在文本文件中输入的位置保存为列表,而不是要求用户在每个位置输入单词代表(与位置列表相同的顺序)结束重新创建句子。 但我不知道什么事情:打开并写入文件
1)我怎样才能使纯文本文件弹出只有当前一个关闭(所以只有当文件:list_of_numbers已关闭,其他文件list_of_words将弹出)。
2)如何将输出写入纯文本文件。
这里是代码:
import subprocess
subprocess.Popen(["notepad","list_of_numbers.txt"])
with open("list_of_numbers.txt","r") as pos_file:
positions = pos_file.read().spllit()
subprocess.Popen(["notepad","list_of_words.txt"])
with open("list_of_words.txt","r") as sentence_file:
words = sentence_file.read().split()
mapping = dict(zip(positions, words))
output = [mapping[position] for position in positions]
print(' '.join(output))
他使用Python3。 '尝试 - 除了'附加?自从什么时候追加提出任何异常? – DahliaSR
@DahliaSR'try-except'不适用于'.append()',它适用于'int()' - 在用户输入非整数的情况下,它不能被转换为int。 (注意,它并不存在“while”字样下。 此外,更新了Python 3的打印语句。我错过了! – cmeadows