好的,基本上,我正在制作一个程序,允许我为即将到来的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")

file.close()print(“Ok!So,”+ subject +“Revision Topics.txt已保存到Documents下的GCSE Revision文件夹。\\ n \\ nGood Luck Revising!”)

结束代码

非常感谢你的帮助,非常感谢<3

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

有任何想法吗?

===============>>#1 票数: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!")

至于你的第二个问题,哪一部分没有写? 该文件是否完全被创建? 如果您使用管理员权限运行该怎么办?

===============>>#2 票数: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

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

  ask by Freddie Worth translate from so

未解决问题?本站智能推荐: