好的,基本上,我正在制作一个程序,允许我为即将到来的GCSE列出修订主题。我的代码如下所示,但是没有写入txt文件! 如果有人有时间,如果你能告诉我怎么做,我也会非常感激: A)使“需要另一个主题”问题无限,直到用户说“n”(否) B)覆盖具有相同名称的现有文件
CODE:
print("Which subject are we doing today?")
subject = input("Subject: ")
file = open(subject + " Revision Topics.txt", "w")
file.write("Here are a list of " + subject + " topics that need revising:\n")
print("Got topics to revise?")
intro = input("y or n: ")
if intro == "y":
print("Which topic needs revising?")
topic1 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic2 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic3 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic4 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic5 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic6 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic7 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic8 = input()
print("Any more?")
anotherone = input("y or n: ")
if anotherone == "y":
print("Which topic also needs revising?")
topic9 = input()
print("You have reached the maximum number of topics.")
elif anotherone == "n":
topic9 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic8 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic7 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic6 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic5 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic4 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic3 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic2 = ("")
else:
print("Answer not recognised! :(")
elif anotherone == "n":
topic1 = ("")
file.write(topic1 + "\n" + topic2 + "\n" + topic3 + "\n" + topic4 + "\n" + topic5 + "\n" +topic6 + "\n" +topic7 + "\n" + topic8 + "\n" +topic9 + "\n")
file.close() print(“Ok!So,”+ subject +“Revision Topics.txt已保存到Documents下的GCSE Revision文件夹中。\ n \ nGood Luck Revising!”)
结束代码
非常感谢您的帮助,非常感谢< 3
更新:我们已经修复了非保存问题,但现在IDLE上的错误告诉我topic9没有定义?但是我在elif中为topic9设置了一个备份,定义为“”
有什么想法吗?
答案 0 :(得分:2)
使用循环法术解决你的第一个问题。
print("Which subject are we doing today?")
subject = input("Subject: ")
wfile = open(subject + " Revision Topics.txt", "w")
wfile.write("Here are a list of " + subject + " topics that need revising:\n")
print("Got topics to revise?")
choice = input("y or n: ")
topics = []
while (choice != 'n'):
print("Which topic needs revising?")
topic = input()
topics.append(topic)
print("Any more?")
choice = input("y or n: ")
wfile.write('\n'.join(topics))
wfile.close()
print("Ok! So, " + subject + " Revision Topics.txt has been saved to the GCSE Revision folder under Documents.\n\nGood Luck Revising!")
关于你的第二个问题,哪一部分没有写?该文件是否完全被创建?如果您使用管理员权限运行该怎么办?
答案 1 :(得分:1)
为了始终确保您的文件正确关闭,使用with open(something) as name
通常要好得多:
with open(subject + " Revision Topics.txt", "w") as output_file:
output_file.write("Here are a list of " + subject + " topics that need revising:\n")
while True:
output_file.write(input("Which topic needs revising?") + '\n')
if input("Any more?") != "y":
break
上述代码将处理文件的打开,写入和关闭 - 使用此方法,您不必记得关闭文件。