帮忙改一下bug!!!
我上一个帖子,是想问一下大家关于模拟支付宝集五福的小程序还有没有什么需要改进的,没想到还是得靠自己想。之后写了一点,然后停不下来了,直到出了bug...本来想写一个限制每日获取福字卡片总数的功能,但是在统计每日卡片总数上出了bug。后续的时间判断也没有继续写了。
想来个哥们儿指正一下bug(最好是能改了),你多指点,我多学习。
上代码:
# coding:utf-8
"""
集五福,迎新春
"""
import random
import time
class Chances(object):
list_chance = []
def __init__(self):
for i in range(0, 90):
self.list_chance.append(0)
for i in range(0, 5):
self.list_chance.append(1)
for i in range(0, 3):
self.list_chance.append(2)
self.list_chance.append(3)
self.list_chance.append(4)
class Blessings(Chances):
cards = 0
money = 0
_blessings = 0
happy = {
"1": {"name": "富强福", "num": 0},
"2": {"name": "和谐福", "num": 0},
"3": {"name": "友善福", "num": 0},
"4": {"name": "爱国福", "num": 0},
"5": {"name": "敬业福", "num": 0}
}
def draw(self, start):
if isinstance(start, str):
number = random.randint(1, 5)
for k, v in self.happy.items():
if str(number) == k:
v["num"] = v["num"] + 1
print("获取到: {}".format(v["name"]))
# 规定每天获取到的福字卡片总数不得超过十张
def check_blessings(self):
for k, v in self.happy.items():
self.cards += v['num']
if self.cards == 11:
print('=' * 20)
raise Exception('每日最多可获取{}张福'.format(self.cards - 1))
def show(self):
print("当前拥有的福:")
for k, v in self.happy.items():
print(v["name"], ":", v["num"])
def service_list(self):
service_list = {
1: '按下<Enter>键集五福',
2: '按下<s>键查看获取福字',
3: '按下<c>合成五福',
4: '按下<d>抽红包',
5: '按下<q>退出'
}
for k, v in service_list.items():
print(v)
def cheak(self):
for k, v in self.happy.items():
if v['num'] > 0:
flag = True
else:
flag = False
break
return flag
def collect(self):
if self.cheak():
self._blessings += 1
print('当前合成{}张五福'.format(self._blessings))
for k, v in self.happy.items():
v['num'] -= 1
else:
print('当前合成{}张五福'.format(self._blessings))
for k, v in self.happy.items():
if v['num'] == 0:
print('缺少{}'.format(v['name']))
print('你的福不足!')
print('=' * 20)
def draw_cash(self):
if self._blessings > 0:
money_flag = random.choice(self.list_chance)
if money_flag == 0:
_money = random.randint(1, 5)
elif money_flag == 1:
_money = random.randint(1000, 1999)
elif money_flag == 2:
_money = random.randint(2000, 4999)
elif money_flag == 3:
_money = random.randint(5000, 9999)
elif money_flag == 4:
_money = random.randint(10000, 20000)
print('抽中红包金额{}'.format(_money))
self.money += _money
print('红包总金额{}'.format(self.money))
self._blessings -= 1
else:
print('五福不足!')
print('=' * 20)
def run(self):
while True:
self.service_list()
start = input("请选择:")
if start == '':
if self.cards <= 10:
self.draw(start)
else:
try:
self.check_blessings()
except Exception as e:
print(e)
print('=' * 20)
elif start == 's':
self.show()
print('=' * 20)
elif start == 'c':
print('=' * 20)
self.collect()
elif start == 'q':
print('感谢使用,再见!')
print('=' * 20)
break
elif start == 'd':
print('=' * 20)
self.draw_cash()
else:
print('输入有误,重新输入!')
print('=' * 20)
if __name__ == '__main__':
b = Blessings()
b.run()
1.在原来代码 116 - 117 行加了一句代码,将次数统计放在这里,每按一次回车就统计一次
2.对原函数 def check_blessings(self): 加以修改
# coding:utf-8
"""
集五福,迎新春
"""
import random
import time
class Chances(object):
list_chance = []
def __init__(self):
for i in range(0, 90):
self.list_chance.append(0)
for i in range(0, 5):
self.list_chance.append(1)
for i in range(0, 3):
self.list_chance.append(2)
self.list_chance.append(3)
self.list_chance.append(4)
class Blessings(Chances):
cards = 0
money = 0
_blessings = 0
happy = {
"1": {"name": "富强福", "num": 0},
"2": {"name": "和谐福", "num": 0},
"3": {"name": "友善福", "num": 0},
"4": {"name": "爱国福", "num": 0},
"5": {"name": "敬业福", "num": 0}
}
def draw(self, start):
if isinstance(start, str):
number = random.randint(1, 5)
for k, v in self.happy.items():
if str(number) == k:
v["num"] = v["num"] + 1
print("获取到: {}".format(v["name"]))
# 规定每天获取到的福字卡片总数不得超过十张
def check_blessings(self):
## for k, v in self.happy.items():
## self.cards += v['num']
## if self.cards == 11:
## print('=' * 20)
## raise Exception('每日最多可获取{}张福'.format(self.cards - 1))
if self.cards > 10:
self.cards = 10
print('=' * 20)
raise Exception('每日最多可获取{}张福'.format(self.cards))
def show(self):
print("当前拥有的福:")
for k, v in self.happy.items():
print(v["name"], ":", v["num"])
def service_list(self):
service_list = {
1: '按下<Enter>键集五福',
2: '按下<s>键查看获取福字',
3: '按下<c>合成五福',
4: '按下<d>抽红包',
5: '按下<q>退出'
}
for k, v in service_list.items():
print(v)
def cheak(self):
for k, v in self.happy.items():
if v['num'] > 0:
flag = True
else:
flag = False
break
return flag
def collect(self):
if self.cheak():
self._blessings += 1
print('当前合成{}张五福'.format(self._blessings))
for k, v in self.happy.items():
v['num'] -= 1
else:
print('当前合成{}张五福'.format(self._blessings))
for k, v in self.happy.items():
if v['num'] == 0:
print('缺少{}'.format(v['name']))
print('你的福不足!')
print('=' * 20)
def draw_cash(self):
if self._blessings > 0:
money_flag = random.choice(self.list_chance)
if money_flag == 0:
_money = random.randint(1, 5)
elif money_flag == 1:
_money = random.randint(1000, 1999)
elif money_flag == 2:
_money = random.randint(2000, 4999)
elif money_flag == 3:
_money = random.randint(5000, 9999)
elif money_flag == 4:
_money = random.randint(10000, 20000)
print('抽中红包金额{}'.format(_money))
self.money += _money
print('红包总金额{}'.format(self.money))
self._blessings -= 1
else:
print('五福不足!')
print('=' * 20)
def run(self):
while True:
self.service_list()
start = input("请选择:")
if start == '':
self.cards += 1
if self.cards <= 10:
self.draw(start)
print(self.cards)
else:
try:
self.check_blessings()
except Exception as e:
print(e)
print('=' * 20)
elif start == 's':
self.show()
print('=' * 20)
elif start == 'c':
print('=' * 20)
self.collect()
elif start == 'q':
print('感谢使用,再见!')
print('=' * 20)
break
elif start == 'd':
print('=' * 20)
self.draw_cash()
else:
print('输入有误,重新输入!')
print('=' * 20)
if __name__ == '__main__':
b = Blessings()
b.run()
页:
[1]