Study/Python
Chapter 09 난생처음 파이썬 프로그래밍 파일 읽기와 쓰기 연습문제
sunkite
2021. 10. 12. 21:46
728x90
반응형
8.
outFile = None
inFile = None
outStr = ""
inStr = ""
idx = 1
inFile = open("C:/FirstPython/Chapter09/normal.txt", "r", encoding="UTF-8")
outFile = open("C:/FirstPython/Chapter09/normal_line.txt", "w")
while True :
inStr = inFile.readline()
if inStr == "" :
break
outFile.writelines(str(idx) + " : " + str(inStr))
idx += 1
inFile.close()
outFile.close()
9.
import turtle
import random
outFile = None
inFile = None
inStr = ""
res = ""
inList = []
outFile = open("C:/FirstPython/Chapter09/turtle.txt", "w")
colorList = ['red', 'blue', 'gray', 'black', 'magenta', 'orange']
turtle.screensize(500,500)
turtle.shape('turtle')
turtle.penup()
turtle.speed(5)
for i in range(50) :
color = random.choice(colorList)
size = random.randint(1,4)
x = random.randint(-250,250)
y = random.randint(-250,250)
angle = random.randint(0,360)
res = color + ", " + str(size) + ", " + str(x) + ", " + str(y) + ", " + str(angle) + ", \n"
outFile.writelines(res)
outFile.close()
inFile = open("C:/FirstPython/Chapter09/turtle.txt", "r", encoding="UTF-8")
inList = inFile.readlines()
for inStr in inList :
turtle.stamp()
turtle.fillcolor(inStr.split(", ")[0])
turtle.pencolor(inStr.split(", ")[0])
turtle.turtlesize(int(inStr.split(", ")[1]))
turtle.goto(int(inStr.split(", ")[2]),int(inStr.split(", ")[3]))
turtle.right(int(inStr.split(", ")[4]))
inFile.close()
turtle.done()
728x90
반응형