Python编程作业

发布于 2024-12-10 05:28:34 字数 1178 浏览 0 评论 0原文

首先感谢您的时间和回答。我的任务是让我的程序打开一个文本文件,读取其数据,使每个单词都是一个不同的字符串,并创建一个 HTML 文档,将这些字符串中的每个字符串显示为随机颜色。所以它几乎希望我们从文本文件中取出每个单词,将每个单词更改为随机颜色,并从中创建一个 HTML 文档。这是我到目前为止的代码:

import random  
def main():
    filename = input("Enter Text File:") 
    infile = open(filename, "r")
    filename2 = input("Enter HTML Document:")
    outfile = open(filename2, "w")
    print("<html>", file=outfile)
    print("  <head>", file=outfile)
    print("  </head>", file=outfile)
    print("  <body>", file=outfile)
    filestring = infile.read()
    file = filestring.split()
    filelength = len(file)
    num = int(random.uniform(0,256))
    num1 = int(random.uniform(0,256))
    num2 = int(random.uniform(0,256))
    i = 0


    for i in range(filelength):
        r = num
        g = num1
        b = num2
        rgb = "{0:02X}{1:02X}{2:02X}".format(r, g, b)
        print('    <span style="color:#{0}">{1}</span>'.format(rgb,                    file[i]),file=outfile)
        i = 0 + 1


    print("  </body>", file=outfile)
    print("</html>", file=outfile)

main()

该代码可以工作,但它不会将每个单词更改为随机颜色,它只是将所有单词更改为相同的颜色。我很欣赏这些答案。

First off thank you for your time and answers. My task is to have my program open a text file, read its data so every word is a different string, and creating a HTML document displaying each one of these strings into a random color. So pretty much it wants us to take every word from a text file change each word into a random color and create a HTML document out of it. This is the code i have so far:

import random  
def main():
    filename = input("Enter Text File:") 
    infile = open(filename, "r")
    filename2 = input("Enter HTML Document:")
    outfile = open(filename2, "w")
    print("<html>", file=outfile)
    print("  <head>", file=outfile)
    print("  </head>", file=outfile)
    print("  <body>", file=outfile)
    filestring = infile.read()
    file = filestring.split()
    filelength = len(file)
    num = int(random.uniform(0,256))
    num1 = int(random.uniform(0,256))
    num2 = int(random.uniform(0,256))
    i = 0


    for i in range(filelength):
        r = num
        g = num1
        b = num2
        rgb = "{0:02X}{1:02X}{2:02X}".format(r, g, b)
        print('    <span style="color:#{0}">{1}</span>'.format(rgb,                    file[i]),file=outfile)
        i = 0 + 1


    print("  </body>", file=outfile)
    print("</html>", file=outfile)

main()

This code works but it does not change each individual word into a random color it just changes all the words into the same color. I appreciate the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰雪之触 2024-12-17 05:28:34

由于这是作业,我将把我的答案限制为一个提示:

您当前正在生成一种随机颜色,并将其应用于每个单词。您应该做的是为每个单词生成新的随机颜色。

Since this is homework, I'll limit my answer to a hint:

You're currently generating one random colour, and applying it to every word. What you should be doing is generating a new random colour for every word.

半步萧音过轻尘 2024-12-17 05:28:34

这段代码应该在循环中(for i in range(filelength):)

num = int(random.uniform(0,256))
num1 = int(random.uniform(0,256))
num2 = int(random.uniform(0,256))

this code should be in the loop (for i in range(filelength):)

num = int(random.uniform(0,256))
num1 = int(random.uniform(0,256))
num2 = int(random.uniform(0,256))
夏日落 2024-12-17 05:28:34

您的代码(我没有执行它)似乎是正确的(忽略异常处理的缺失)。您需要更改每个单词的 numnum1num2。这意味着,您需要将 numnum1num2 放入循环内 for i in range(filelength):.

Your code (I haven't executed it) seems to be correct(ignoring the absence of Exception Handling). You need to change num, num1 and num2 for each word. That would mean, you need to put num, num1 and num2 inside the loop for i in range(filelength):.

那小子欠揍 2024-12-17 05:28:34

试试这个:

import random  
def main():
    filename = input("Enter Text File:").strip()
    infile = open(filename, "r")
    filename2 = input("Enter HTML Document:").strip()
    outfile = open(filename2, "w")
    print("<html><head></head><body>",file=outfile)
    for line in infile:
        for word in line.split():
            (r,g,b)=[int(random.uniform(0,256)) for x in range(3)]
            rgb = "{0:02X}{1:02X}{2:02X}".format(r, g, b)
            print('    <span style="color:#{0}">{1} </span>'.format(rgb,word),file=outfile)
        print("<br>",file=outfile)

    print("  </body>", file=outfile)
    print("</html>", file=outfile)

main()

Try this:

import random  
def main():
    filename = input("Enter Text File:").strip()
    infile = open(filename, "r")
    filename2 = input("Enter HTML Document:").strip()
    outfile = open(filename2, "w")
    print("<html><head></head><body>",file=outfile)
    for line in infile:
        for word in line.split():
            (r,g,b)=[int(random.uniform(0,256)) for x in range(3)]
            rgb = "{0:02X}{1:02X}{2:02X}".format(r, g, b)
            print('    <span style="color:#{0}">{1} </span>'.format(rgb,word),file=outfile)
        print("<br>",file=outfile)

    print("  </body>", file=outfile)
    print("</html>", file=outfile)

main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文