帮忙解决一下bug

帮忙解决一下bug

问题描述:加了一个每日‘集五福’上限,即规定每天获取到的福字卡片总数不得超过十张,否则就要等到第二天才能继续进行‘集五福’获取福字卡片。可没有达到预期效果,定义的self.cards用来计算每天获取的福字卡片总数,一直为0。

​# 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回答

同学,你好!当start为空字符串时,执行if中的代码,并没有修改self.cards的值,内层的if判断一直成立,不会执行else中的代码,因此self.cards的值始终为0

http://img1.sycdn.imooc.com//climg/60ea4d7a0970eea006170547.jpg

每次执行都需要调用self.check_blessings()判断self.cards的值,若条件成立输出异常即可

http://img1.sycdn.imooc.com//climg/60ea537609524b3305440425.jpg

每次获取到福卡时,修改self.cards的值,check_blessings()中不需要再修改self.cards的值了

http://img1.sycdn.imooc.com//climg/60ea53a70909dd7807480264.jpg

http://img1.sycdn.imooc.com//climg/60ea4e3d09f2c03f09780228.jpg


祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师