From bc95ef1ae8b4cb727368bdca05b7467d361cccf4 Mon Sep 17 00:00:00 2001 From: "Hancel.Lin" Date: Thu, 12 Apr 2018 10:55:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E9=A1=B9=E7=9B=AE=E4=BA=8CPy?= =?UTF-8?q?thon=E7=89=88=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- 02/backend/main_binary.py | 108 ++++++++++++++++++++++++++++++++++++++ 02/backend/main_json.py | 107 +++++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 02/backend/main_binary.py create mode 100644 02/backend/main_json.py diff --git a/.gitignore b/.gitignore index 490b385..abfd49c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules *.out *.pyc package-lock.json -.vscode \ No newline at end of file +.vscode +*.bin \ No newline at end of file diff --git a/02/backend/main_binary.py b/02/backend/main_binary.py new file mode 100644 index 0000000..51dec67 --- /dev/null +++ b/02/backend/main_binary.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import pickle +import os +import sys +import time +from random import random + +save_path = 'data.bin' + +def set_name(data): + print('请输入你的名字:') + data['name'] = raw_input().decode(sys.stdin.encoding) + return save(data) + +def guess(data): + name = data['name'] + rand_number = int(random() * 1000 + 1) + guess_number = 0 + count = 0 + start = int(time.time()) + + print('请输入1~1000的数字:') + while rand_number != guess_number: + guess_number = input() + if guess_number < rand_number: + print('小了') + elif guess_number > rand_number: + print('大了') + count += 1 + cost = int(time.time() - start) + print('恭喜你!猜中了!你一共猜测了{}次'.format(count)) + + + if not name: + print('这是你第一次玩猜数字。') + data = set_name(data) + + data['records'].append({ + 'name': data['name'], + 'number': rand_number, + 'count': count, + 'start': start, + 'cost': cost + }) + return data + +def load(path): + data = { + 'name': '', + 'records': [] + } + if os.path.exists(path): + with open(path, 'r') as fp: + data = pickle.load(fp) + return data + +def save(data): + with open(save_path, 'w') as fp: + pickle.dump(data, fp) + return data + +def view(data): + if data['records']: + print('姓名\t所猜数字\t猜测次数\t花费秒数\t开始时间') + for record in data['records']: + print(u'{name}\t{number}\t\t{count}\t\t{cost}\t\t{start}'.format( + name = record['name'], + number = record['number'], + count = record['count'], + cost = record['cost'], + start=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(record['start'])) + )) + else: + print('你还没有玩过呢。') + print('') + return data + +def menu(name): + if name: + print(u'你好!{name}!'.format(name=name)) + print('欢迎来玩『 猜数字 』!') + print('') + print('1. 开始游戏!') + print('2. 查看历史!') + print('3. 更改昵称!') + print('4. 退出游戏!') + print('') + + return raw_input() + + +def main(): + data = load(save_path) + fun_dict = { + '1': lambda: save(guess(data)), + '2': lambda: view(data), + '3': lambda: set_name(data), + '4': exit + } + while True: + cmd = menu(data['name']) + fun = fun_dict.get(cmd, None) + if fun: + data = fun() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/02/backend/main_json.py b/02/backend/main_json.py new file mode 100644 index 0000000..8a39b36 --- /dev/null +++ b/02/backend/main_json.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import json +import os +import sys +import time +from random import random + +save_path = 'data.json' + +def set_name(data): + print('请输入你的名字:') + data['name'] = raw_input().decode(sys.stdin.encoding) + return save(data) + +def guess(data): + name = data['name'] + rand_number = int(random() * 1000 + 1) + guess_number = 0 + count = 0 + start = int(time.time()) + + print('请输入1~1000的数字:') + while rand_number != guess_number: + guess_number = input() + if guess_number < rand_number: + print('小了') + elif guess_number > rand_number: + print('大了') + count += 1 + cost = int(time.time() - start) + print('恭喜你!猜中了!你一共猜测了{}次'.format(count)) + + if not name: + print('这是你第一次玩猜数字。') + data = set_name(data) + + data['records'].append({ + 'name': data['name'], + 'number': rand_number, + 'count': count, + 'start': start, + 'cost': cost + }) + return data + +def load(path): + data = { + 'name': '', + 'records': [] + } + if os.path.exists(path): + with open(path, 'r') as fp: + data = json.load(fp) + return data + +def save(data): + with open(save_path, 'w') as fp: + json.dump(data, fp) + return data + +def view(data): + if data['records']: + print('姓名\t所猜数字\t猜测次数\t花费秒数\t开始时间') + for record in data['records']: + print(u'{name}\t{number}\t\t{count}\t\t{cost}\t\t{start}'.format( + name = record['name'], + number = record['number'], + count = record['count'], + cost = record['cost'], + start=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(record['start'])) + )) + else: + print('你还没有玩过呢。') + print('') + return data + +def menu(name): + if name: + print(u'你好!{name}!'.format(name=name)) + print('欢迎来玩『 猜数字 』!') + print('') + print('1. 开始游戏!') + print('2. 查看历史!') + print('3. 更改昵称!') + print('4. 退出游戏!') + print('') + + return raw_input() + + +def main(): + data = load(save_path) + fun_dict = { + '1': lambda: save(guess(data)), + '2': lambda: view(data), + '3': lambda: set_name(data), + '4': exit + } + while True: + cmd = menu(data['name']) + fun = fun_dict.get(cmd, None) + if fun: + data = fun() + +if __name__ == '__main__': + main() \ No newline at end of file