«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Code IT

Python - 실습 - 게시판 만들기 (함수) 본문

Python & Django

Python - 실습 - 게시판 만들기 (함수)

Codit 2019. 1. 31. 15:37
articles = []

def print_welcome_message():
	print("""############################
# Welcome to Python Boards #
############################""")

def print_menu():
	print("""##################################################################
# 1. List # 2. Search # 3. Read # 4. Delete # 5. Write # 0. Quit #
##################################################################""")	

def input_menu():
	id = input("Choice Menu : ")
	return int(id)
	
def print_all_articles():
	if len(articles) == 0:
		print("Have no articles.")
		return
	
	for artcl in articles:
		print_article(artcl)

def print_article(article):
	print("Subject : " + article["subject"] + ", Read : " + str(article["read"]))

def search_article():
	search_keyword = input("Input search keyword : ")
	
	for artcl in articles:
		if search_keyword in artcl["subject"]:
			print_article(artcl)
				
def read_article():
	id = input("Choice article id : ")
	id = int(id)
	
	if len(articles) - 1 < id:
		print(id, " isn't exists.'")
	else:
		articles[id]["read"] += 1
		print_article(articles[id])

def delete_article():
	id = input("Choice article id : ")
	id = int(id)
	
	if len(articles) - 1 < id:
		print(id, " isn't exists.'")
	else:
		articles.pop(id)

def write_article():
	subject = input("Input Subject : ")
	article = {
		"subject": subject,
		"read": 0
	}
	
	articles.append(article)
	
if __name__ == "__main__":
	print_welcome_message()
	
	while(True):
		print_menu()
		id = input_menu()
		
		if id == 1:
			print_all_articles()
		elif id == 2:
			search_article()
		elif id == 3:
			read_article()
		elif id == 4:
			delete_article()
		elif id == 5:
			write_article()
		elif id == 0:
			print("Quit!")
			break
		print("")

'Python & Django' 카테고리의 다른 글

Python - self  (0) 2019.02.03
Python - 실습 - 게시판 만들기 (클래스)  (0) 2019.01.31
Python - class  (0) 2019.01.31
Python - loop ~ else  (0) 2019.01.31
Python - for  (0) 2019.01.31
Comments