我需要将rand_Num添加到名为Rand_Number的空白txt文件中。我需要做100000次。这是我的代码... #导入所需的库
import random
import turtle
# screen variables
wn_wid = 550
wn_length = 350
wn_title = "Random output"
wn_background_col = "#3d3d3d"
wn_tracer = 0
# creates and configures window
wn = turtle.Screen()
wn.setup(wn_wid, wn_length)
wn.title(wn_title)
wn.bgcolor(wn_background_col)
wn.tracer(wn_tracer)
# creates text
text = turtle.Turtle()
text.speed(0)
text.color("#ffffff")
text.pu()
text.hideturtle()
# text variables
font = "Comic Sans MS"
font_size = 48
alignment = "center"
# rep_num variable
rep_Num = 0
# central loop
# loop repeat variable
repeats = 100000
# main loop
for reps in range(repeats):
# adds 1 to rep_Num variable
rep_Num += 1
# generates random number
rand_Num = random.randint(0, 100)
# puts text on screen
text.clear()
text.write(str(rep_Num) + ": " + str(rand_Num), align=alignment, font=(font, font_size, "normal"))
# checks to see whether it needs to close win
if rep_Num == repeats:
wn.bye()
break
# updates window
wn.update()
这就是我的全部代码,并且都可以按我的要求工作。我只需要在循环中做一些事情,就可以让我将rand_Num变量添加到名为Rand_Numbers的txt中的新行中
答案 0 :(得分:1)
我使用CSV模块完成此操作,这将使您可以随着项目的增长轻松添加其他数据。即使这不是您的意图,生成的文件仍可以轻松用于数据分析目的。如果确实需要,也可以通过这种方式生成文本文件。
我添加了此功能:
def write_random(rand_Num):
num = [rand_Num]
with open("random_values.csv", "a", newline='', encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(num)
它会生成一个csv文件(如果您想对数据或其他内容进行数据分析,则可以通过Excel处理),并将每个随机值放在单独的一行上。
如果您确实想要一个文本文件,则只需将扩展名从.csv更改为.txt,只要每行仅包含一个数据即可。
这是完整的工作代码:
import random
import turtle
import csv
def write_random(rand_Num):
num = [rand_Num]
with open("Rand_Number.csv", "a", newline='', encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(num)
# screen variables
wn_wid = 550
wn_length = 350
wn_title = "Random output"
wn_background_col = "#3d3d3d"
wn_tracer = 0
# creates and configures window
wn = turtle.Screen()
wn.setup(wn_wid, wn_length)
wn.title(wn_title)
wn.bgcolor(wn_background_col)
wn.tracer(wn_tracer)
# creates text
text = turtle.Turtle()
text.speed(0)
text.color("#ffffff")
text.pu()
text.hideturtle()
# text variables
font = "Comic Sans MS"
font_size = 48
alignment = "center"
# rep_num variable
rep_Num = 0
# central loop
# loop repeat variable
repeats = 100000
# main loop
for reps in range(repeats):
# adds 1 to rep_Num variable
rep_Num += 1
# generates random number
rand_Num = random.randint(0, 100)
write_random(rand_Num)
# puts text on screen
text.clear()
text.write(str(rep_Num) + ": " + str(rand_Num), align=alignment, font=(font, font_size, "normal"))
# checks to see whether it needs to close win
if rep_Num == repeats:
wn.bye()
break
# updates window
wn.update()
答案 1 :(得分:0)
您可以使用with open(file)
Synthax。我会写这样的东西:
with open('Rand_Numbers', 'w') as f:
for reps in range(repeats):
# adds 1 to rep_Num variable
rep_Num += 1
# generates random number
rand_Num = random.randint(0, 100)
f.write(str(rand_Num))
# puts text on screen
text.clear()
text.write(str(rep_Num) + ": " + str(rand_Num), align=alignment, font=(font, font_size, "normal"))
# checks to see whether it needs to close win
if rep_Num == repeats:
wn.bye()
break
# updates window
wn.update()
顺便说一句,您应该删除rep_Num = 0
和
if rep_Num == repeats:
wn.bye()
break
在您的代码中,因为循环已经完成了工作。
wn.bye()
将在循环后添加。