[python] Day 23
# score_file = open("score.txt", "w", encoding="utf8")
# print("math:20", file=score_file)
# print("b:30", file=score_file)
# score_file.close()
# 반복문을 통해서 줄의 갯수를 알 수 있음
# score_file = open("score.txt", "r", encoding="utf8")
# lines = score_file.readlines()
# for line in lines:
# print(line)
# pickle
import pickle
profile_file = open("profile.pickle","wb")
profile = {"name":"a", "age":"b", "location":"c"}
print(profile)
pickle.dump(profile, profile_file)
profile_file.close()
# bring the content from the file
profile_file = open("profile.pickle","rb")
profile = pickle.load(profile_file)
print(profile)
profile_file.close()
파일 처리
with open("study.txt","w", encoding = "utf8") as study_file:
study_file.write("python is cool")
with open("study.txt","r", encoding ="utf8") as study_file:
print(study_file.read()
# 보고서 만들기
for i in range(1, 51):
with open(str(i) + "th report", "w") as report_csv:
report_csv.write("-{0}th week report-".format(i))
report_csv.write("\nname:")
report_csv.write("\ndate:")
#랜덤 숫자
from random import *
print(int(random()*10)+1)
#not including 45
print(randrange(1,45)+1) #not including 45
print(randint(1,45)) #including
class 만들어보기
name = "marine"
age = 40
damage = 5
print(
"{0} unit has been created that is {1} years old and has {2} damage".format(
name, age, damage))
tank_name = "tank"
tank_age = 150
tank_damage = 35
print(
"{0} unit has been created that is {1} years old and has {2} damage".format(
tank_name, tank_age, tank_damage))
def attack(name, age, damage):
print("{0}{1}{2}".format(name, age, damage))
attack(name, "1시", damage)
class Unit:
# innit은 생성자로 객체를 만들어줌 - 자동 호출임 객체란 클래스로부터 만들어지는 녀석들; 고로 마린1과 탱크는 유닛 클래스의 인스턴스임
def __init__(self, name, hp, damage):
self.name= name
self.hp= hp
self.damage=damage
print("{0} is created".format(self.name))
print("hp: {0} damage:{1}".format(self.hp, self.damage))
marine1= Unit("marine1", 20, 3)
tank= Unit("tank", 10, 33)
class unit:
# innit은 생성자로 객체를 만들어줌 - 자동 호출임 객체란 클래스로부터 만들어지는 녀석들; 고로 마린1과 탱크는 유닛 클래스의 인스턴스임
def __init__(self, name, hp, damage):
self.name= name
self.hp= hp
self.damage=damage
print("{0} is created".format(self.name))
print("hp: {0} damage:{1}".format(self.hp, self.damage))
marine1= unit("marine1", 20, 3)
tank= unit("tank", 10, 33)
#함수 밖에서 유닛을 쓸 때
wraith= unit("wraith", 80,5)
print("unit name: {0}, hp={1}, damage={2}".format(wraith.name, wraith.hp, wraith.damage))
wraith2= unit("stolen wraith",2,30)
#딕셔너리
player = {
"name": "nico",
"age": 22,
"xe": 3,
"favFood": ["pizza","burger"],
"friend": {
"name": "rick",
"age": 23,
"xp": 3,
"favFood": ["pizza","burger"]
}
}
player["favFood"]="2"
player.pop("xe")
player["friend"]["favFood"].append("2")
print(player)
# }
# player.pop("age")
# player["xp"] = 1500
# print(player)
# player["favFood"].append("noodle")
# print(player)
# list는 numbers점찍고.list, count, append 등등 다양하게 쓸 수 있음