【问题标题】:I can't open a HTML Customized page using Python for some reason由于某种原因,我无法使用 Python 打开 HTML 自定义页面
【发布时间】:2013-12-25 21:11:34
【问题描述】:

到目前为止,这是我的代码:

import os

print("The Arkeyan Text to HTML Converter\n\n")

w = 1

while w == 1:
   var1 = input("\n\nWhat would you like the title of your page to be? ")
   var2 = input("\nCool! Okay, now what is the text you want inside the body? ")
   align = input("\nHow would you like this to be aligned? (center, right or left) ")
   background = input("\nFinally, what background colour would you like? (It has to be in RGB format). Press 9 for more info.")

   if background == "9":
      background = input("\nGo to http://www.w3schools.com/tags/ref_colorpicker.asp to get a colour value and enter it in here: ")
      print("\n\nAll done! Here is your personalised code:\n\n")
      code = print("<!Doctype html>\n\
      <html>\n\
      <head>\n\
            <title>",var1,"</title>\n\
      </head>\n\n\n\
      <body bgcolor=\"",background,"\">\n\
      <div align=\"",align,"\">\n\
      <h1>",var1,"</h1><br>\n\
      <p>",var2,"</p>\n\
      </div>\n\
      </body>\n\
      </html>")

      my_file = open("C:\output.html", "w")

      my_file.write(str(code))

      my_file.close()

      os.system("C:\\output.html")

      x = input("\n\nThank you for using this HTML convert tool. Would you like to generate another code? ")
      if x == "yes":
         print("\n\n==========UNDERGOING LOOP PROCESS==========\n")
      elif x == "no":
         print("\n\n==========BREAKING THE LOOP==========\n")
         w = 2
      else:
         print("I don't understand you. Self-destruct sequence initaiated...")
         exit()

   else:

      print("\n\nAll done! Here is your personalised code:\n\n")
      code = print("<!Doctype html>\n\
      <html>\n\
      <head>\n\
            <title>",var1,"</title>\n\
      </head>\n\n\n\
      <body bgcolor=\"",background,"\">\n\
      <div align=\"",align,"\">\n\
      <h1>",var1,"</h1><br>\n\
      <p>",var2,"</p>\n\
      </div>\n\
      </body>\n\
      </html>")

      my_file = open("C:\output.html", "w")

      my_file.write(str(code))

      my_file.close()

      os.system("C:\\output.html")

      x = input("\n\nThank you for using this HTML convert tool. Would you like to generate another code? ")
      if x == "yes":
         print("\n\n==========UNDERGOING LOOP PROCESS==========\n")
      elif x == "no":
         print("\n\n==========BREAKING THE LOOP==========\n")
         w = 2
      else:
         print("I don't understand you. Self-destruct sequence initaiated...")
         exit()

HTML 页面可以正常打开,但显示“无”!这是为什么?我检查了几次,似乎无法解决问题。你能帮助我吗?附言我需要很快修复我的错误;我必须尽快提交!

【问题讨论】:

    标签: python html operating-system converter


    【解决方案1】:

    你正在分配

    print
    

    到“代码”(打印返回无)。

    【讨论】:

    • 所以我删除了打印元素?
    • 在:code = print("\n\ ...删除"code="
    【解决方案2】:

    print 不返回任何内容,因此code = print(...) 意味着code 将始终为None。

    如果您希望 code 成为字符串,请删除对 print 的调用。您还需要使用不同的方法来格式化您的字符串。基本的+ 连接有效,或者(更好)使用字符串格式:

    'this is a {} string'.format('formatted')
    #this is a formatted string
    

    在docs阅读更多内容。

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 2018-10-16
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      相关资源
      最近更新 更多