我卡住了。我检查了整个代码,没有发现任何错误

2024-09-30 18:32:56 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,基本上,我正在做一个程序,允许我列出即将到来的GCSE的复习主题。我的代码如下所示,但是txt文件没有被写入!! 如果有人有时间,如果您能告诉我如何: A) 使“需要另一个主题”问题无限大,直到用户说“n”(否) B) 覆盖同名的现有文件

代码:

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")

你知道吗文件.close() 打印(“好!所以,“+主题+”修订主题.txt已保存到“文档”下的GCSE修订文件夹中。\n\n“好!”)你知道吗

结束代码

非常感谢您的帮助,非常感谢<;3

更新:我们已经修复了不保存的问题,但是现在IDLE上的错误告诉我topic9没有定义?但是,我在elif中为topic9设置了一个备份,将其定义为“”

有什么想法吗?你知道吗


Tags: oranswerwhichinputtopicifmoreany
2条回答

为了始终确保文件正确关闭,通常最好使用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

上面的代码将处理文件的打开、写入和关闭—使用此方法,您不必记住关闭文件。你知道吗

使用循环的魔力来解决你的第一个问题。你知道吗

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!")

至于你的第二个问题,哪一部分没有写?文件是否正在创建?如果你用管理员权限运行呢?你知道吗

相关问题 更多 >