|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 TsaiYen 于 2020-7-12 19:38 编辑
鱼友们可以帮我看看代码哪里需要改进吗?
感觉自己写得太复杂,很烂...
- from random import randint
- from time import sleep
- from terminaltables import AsciiTable
- Head = ["回合", " 范围", "猜谜数字"]
- Min, Max, Round = 1, 100, 1
- isBingo, Judy = True, True
- data = []
- #选择模式
- def mode():
- body = "电脑随机生成终极密码(1) or 手动设定终极密码(2) or 退出当前操作(3): "
- while Judy:
- print("------------------------终极密码1 ~ 100 ------------------ ---------")
- choose = input(body)
- if choose.isdigit() and 0 < int(choose) < 4:
- if choose == "1":
- generate_num()
- if choose == "2":
- game_start()
- if choose == "3":
- break
- else:
- print("输入指令错误")
- body = "重新输入指令"
- #计算机随机产生密码
- def generate_num():
- global Min, Max, Judy, Round
- #初始化范围
- Min, Max, Round = 1, 100, Round
- #不包含 1 和 100
- r = randint(2, 100)
- print(r)
- print("正在产生密码...")
- sleep(3)
- print("终极密码产生完毕 ! ! !")
- while True:
- result = input("输入终极密码,密码介于 {} ~ {} : \n".format(Min, Max))
- data.append([Round, "%3d ~ %3d" % (Min, Max), result])
- Round += 1
- if result.isdigit() and Min < int(result) < Max:
- result = int(result)
- if result > r:
- Max = result
- elif result < r:
- Min = result
- else:
- print("Bingo,答对了 ! ! ! ")
- table = AsciiTable([Head, *data])
- print(table.table)
- if input("还要继续玩吗 [y / n] ?: ") == "y":
- break
- else:
- print("游戏结束 ! ! !")
- Judy = False
- break
- else:
- print("密码并非数字 & 终极密码范围超出 ! ! !\n重新输入终极密码...")
- #游戏开始,手动设定密码
- def game_start():
- global Min, Max, isBingo, Judy, Round
- #初始化范围
- Min, Max, Round = 1, 100, 1
- #初始化 isBingo
- isBingo = True
- while isBingo:
- num = int(input("设定终极密码: "))
- if isinstance(num, int) and Min < num < Max :
- print("设定完成")
- while True:
- result = input("输入终极密码,密码介于 {} ~ {} : \n".format(Min, Max))
- data.append([Round, "%3d ~ %3d" % (Min, Max), result])
- Round += 1
- if result.isdigit() and Min < int(result) < Max:
- result = int(result)
- if result > num:
- Max = result
- elif result < num:
- Min = result
- else:
- print("Bingo,答对了 ! ! ! ")
- table = AsciiTable([Head, *data])
- print(table.table)
- if input("还要继续玩吗 [y / n] ?: ") == "y":
- isBingo = False
- break
- else:
- isBingo = False
- Judy = False
- print("游戏结束 ! ! !")
- break
- else:
- print("密码并非数字 & 终极密码范围超出 ! ! !\n重新设定终极密码...")
- else:
- print("重新设定 ! 密码并非数字 & 范围超出")
-
- mode()
-
复制代码 |
|