我的代码有问题,我的代码是用来创建一个文件,并写入一个单词列表和一个数字列表到文件。代码根本不创建文件。这里是:
sentence=input('please enter a sentence: ')
list_of_words=sentence.split()
words_with_numbers=enumerate(list_of_words, start=1)
filename = 'fileoflists.txt'
with open('fileoflists', 'w+') as file:
file.write(str(list_of_words) + '/n' + str(words_with_numbers) + '/n')
谢谢
引用this question for info。试试这个:
运行代码时,它确实创建了文件,但是可以看到您在
filename
中用"fileoflists.txt"
的值定义了文件名,但是您不使用该参数,只创建了一个文件(不是文本文件)。在它也不会打印出你期望的结果。对于list,它打印列表的字符串表示,但是对于
words_with_numbers
,它打印由enumerate
返回的迭代器的__str__
。在参见以下代码更改:
相关问题 更多 >
编程相关推荐