赞
踩
输入1 可以持续添加数字,
输入2可以查看所有数字
输入数字0,退出系统
进行需求分析,编写简单的代码实现功能(本练习非常基础,有待完善)
- import json
- import os.path
- import time
-
- menu = """输入1 可以持续添加数字,
- 输入2可以查看所有数字
- 输入数字0,退出系统"""
-
- items = []
- FILE_PATH = "./items.json"
-
-
- def save_data(): #存储
- with open(FILE_PATH, "w") as f:
- json.dump(items, f)
-
-
- def load_data():
- if os.path.exists(FILE_PATH): #加载
- with open(FILE_PATH, "r") as f:
- for data in json.load(f):
- items.append(data)
-
-
- def check_option():
- while True:
- print(menu)
- option = input("请输入选项")
- if option not in ["0", "1", "2"]:#简单判断用户输入
- print(f"输入不合法!重新输入")
- return option
-
-
- def add_num():
- while True:
- value = int(input("添加数字:"))
- items.append(value)
- print(f"添加成功!")
- save_data()
- while True:
- select_yn = input("继续添加(y),退至菜单(n)")#判断用户是否继续输入
- if select_yn not in ["y", "Y", "n", "N"]:
- print(f"输入不合法!")
- elif select_yn == "y" or select_yn == "Y":
- break
- elif select_yn == "n" or select_yn == "N":
- break
- if select_yn == "n" or select_yn == "N":
- break
-
-
-
-
- def look_all_num():
- if items: #进行简单的判断items是否为空
- for item in items:
- print(item)
- else:
- print(f"还没有添加数字")
-
-
- def quit_system():
- print(f"退出系统中。。。")
- time.sleep(2) #简单的系统休眠时间
- print(f"退出成功!")
-
-
- load_data()
-
-
- def main():
- while True:
- option = check_option()
- if option == "1":
- while True:
- add_num()
- elif option == "2":
- look_all_num()
- elif option == "0":
- quit_system()
- break
-
-
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。