问题是“w”输出和python3中的循环

时间:2013-10-24 12:30:39

标签: python python-3.x

我目前正在编写的程序存在问题。

以下是代码:

def main():

    print ("This program let you create your own HTML-page,\nwith the necessary tags allready included")

    t = ("<!DOCTYPE html>", "<html>", " <head>", " </head>", "<body>", "</html>") #Spaces for the indentation in the HTML-code.

    menu = input("Press 1 to enter the file name for the html-page\nPress 2 to enter title for the HTML-page\nPress 3 to start entering code in body ") 

    while True:
        if menu == "1":
            name = input("Enter the name for your HTML-page: ") 
            #with open(name + ".html", 'w') as doc: 
            #Here is the first problem, indenterror if uncommented, otherwise elif gets syntax error. 
            #And this is crucial to get the code into the HTML-document.
            #The error without (with open(name + ".html", 'w') as doc:) will be "global name "doc" is not defined".
            menu = input("Press 2 to enter title for the HTML-page\nPress 3 to start entering code in body ")
        elif menu == "2":
            print (t[0], file=doc) #<!DOCTYPE html>
            print (t[1], file=doc) #<html>
            print (t[2], file=doc) #<head>
            title = input("Enter your title here: ")
            doc.write(title)
            print (t[3], file=doc) #</head>
            menu = input("Press 3 to start entering code in body ")
        elif menu == "3":
            print(t[4], file=doc) #<body>
            code = input("Type </body> to finish your html-page and to close the program.\nEnter your code for body below:\n")
            doc.write('{0}\n'.format(code)) #For newline to get the HTML-code cleaner.
            while code != "</body>":
                code = input("")   
                doc.write('{0}\n'.format(code)) 
            if code == "</body>":
                print ("Congratulations! You have successfully created your own HTML-page by using this python program.")
                print (t[5], file=doc) #</html> 
                #somewhere in the loop above is the second error, I want the </body> to end the program,
                #but it loops line 25 (code = input("Type </body> to finish your html-page and to close the program.\nEnter your code for body below:\n"))


main ()

现在问题。正如您所看到的,我正在尝试编写一个菜单供用户从3个不同的任务中进行选择。他们所做的一切都应该输出到.html文档。

我已经在代码中评论了我的问题,你可以看到。

我无法弄清楚如何在没有elif缩进的情况下获取with open(name + ".html", 'w') as doc:,或者我只是得到elif的语法错误。

第二个问题是我的循环结束。我希望命令退出程序,因为它还输出.html文档的正确结束代码,但它循环code = input("Type </body> to finish your html-page and to close the program.\nEnter your code for body below:\n"),我也无法解决这个问题。

2 个答案:

答案 0 :(得分:2)

def main():

    (...)
    while True:
        if menu == "1":
            name = input("Enter the name for your HTML-page: ")
            doc = open(name + ".html", 'w')
            menu = input("Press 2 to enter title for the HTML-page\nPress 3 to start entering code in body ")

    (...)
    doc.close()


main ()

您可以打开如下文件:doc = open(name + ".html", 'w'),但不要忘记在完成后将其关闭,例如doc.close()

答案 1 :(得分:1)

这是一个简单的文件打开功能。当然,需要根据您的特定需求进行调整。 “with”语句本身将关闭文件。

def cat(openfile): #Emulates the cat command used in the Unix shell#
    with open(openfile) as file:
        lines = file.readlines()
        return ''.join(lines)

如果要写入文件,请使用此功能。

def write2file(openfile, WRITE): #openfile = filename to open #WRITE = the string you want to write to file
    with open(openfile, 'w') as file:
        file.write(str(WRITE))

将文本追加/添加到文件中:

def add2file(openfile, add):
    with open(openfile, 'a') as file:
        file.write(str(add))