2013-10-24 63 views
1

我有一個問題,我正在寫的程序。問題與「w」輸出和循環python3

下面的代碼:

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文檔。

我已經在代碼中評論了我的問題,你可以看到。

我不知道如何得到with open(name + ".html", 'w') as doc:沒有縮進elif越來越亂了,或者我只是得到一個elif語法錯誤。

第二個問題是我的循環結束。我想讓命令退出程序,因爲它也爲.html文件輸出正確的結束碼,但它循環code = input("Type </body> to finish your html-page and to close the program.\nEnter your code for body below:\n"),我無法弄清楚。

回答

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

+0

或在首位使用'with'語句,所以它會自動關閉。 – glglgl

+0

@glglgl呃,他有縮進錯誤,所以我認爲他會想以舊式的方式去做。但是,是的,'與'是好得多。而且,這樣做會是一個問題,因爲他的代碼是以一種奇怪的方式構造的。 –

+0

Oomph。我看到你的答案沒有真正閱讀這個問題。對不起,噪音。 – glglgl

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