Python编程作业
首先感谢您的时间和回答。我的任务是让我的程序打开一个文本文件,读取其数据,使每个单词都是一个不同的字符串,并创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于这是作业,我将把我的答案限制为一个提示:
您当前正在生成一种随机颜色,并将其应用于每个单词。您应该做的是为每个单词生成新的随机颜色。
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.
这段代码应该在循环中(for i in range(filelength):)
this code should be in the loop (for i in range(filelength):)
您的代码(我没有执行它)似乎是正确的(忽略异常处理的缺失)。您需要更改每个单词的
num
、num1
和num2
。这意味着,您需要将num
、num1
和num2
放入循环内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
andnum2
for each word. That would mean, you need to putnum
,num1
andnum2
inside the loopfor i in range(filelength):
.试试这个:
Try this: