修改这段代码def main(): print('欢迎来到游戏商店') print('*'*40) gold = 5000 weapon_list=[] print('当前的金币为',gold) while True: print("1,购买法系装备") print("2,购买物理装备") print("3,删除装备") print("4,查看装备") print("5,退出") choice = int(input('请输入你的选择:')) if choice == 1: print('欢迎进入法系装备库') weapons = [['审判系列', 1000], ['帝国系列', 2950], ['圣光系列', 3400], ['幻系列', 4800]] for weapon in weapons: print(weapons) weaponname = input('请输入要购买的装备名称:') if weaponname not in weapon_list: for weapon in weapons: if weaponname == weapon[0]: if gold >= weapon[1]: gold -= weapon[1] weapon_list.append(weapon[0]) print('购买装备',weapon) else: print('金币不足,赶快充值去吧!') break else: print('输入装备名称错误') break
时间: 2024-02-10 22:32:53 浏览: 146
好的,我来修改这段代码。首先,应该将打印所有武器的代码移出循环,将其放在循环之前。其次,应该将购买装备的代码放在一个单独的函数中,以便重复使用和代码复用。最后,应该在其他选项下添加代码,以实现它们的功能。修改后的代码如下:
```python
def buy_weapon(gold, weapon_list, weapons):
weaponname = input('请输入要购买的装备名称:')
for weapon in weapons:
if weaponname == weapon[0]:
if gold >= weapon[1]:
gold -= weapon[1]
weapon_list.append(weapon[0])
print('购买装备', weapon)
else:
print('金币不足,赶快充值去吧!')
break
else:
print('输入装备名称错误')
def main():
print('欢迎来到游戏商店')
print('*' * 40)
gold = 5000
weapon_list = []
print('当前的金币为', gold)
while True:
print("1,购买法系装备")
print("2,购买物理装备")
print("3,删除装备")
print("4,查看装备")
print("5,退出")
choice = int(input('请输入你的选择:'))
if choice == 1:
print('欢迎进入法系装备库')
weapons = [['审判系列', 1000], ['帝国系列', 2950], ['圣光系列', 3400], ['幻系列', 4800]]
for weapon in weapons:
print(weapon)
buy_weapon(gold, weapon_list, weapons)
elif choice == 2:
print('欢迎进入物理装备库')
weapons = [['战狂系列', 1200], ['斩魄系列', 3200], ['破晓系列', 3800], ['狂怒系列', 5100]]
for weapon in weapons:
print(weapon)
buy_weapon(gold, weapon_list, weapons)
elif choice == 3:
print('欢迎进入删除装备界面')
if len(weapon_list) == 0:
print('您的装备列表为空!')
else:
weaponname = input('请输入要删除的装备名称:')
if weaponname in weapon_list:
weapon_list.remove(weaponname)
print('删除装备', weaponname)
else:
print('您的装备列表中不存在该装备!')
elif choice == 4:
print('欢迎进入查看装备界面')
if len(weapon_list) == 0:
print('您的装备列表为空!')
else:
print('您当前拥有的装备为:', weapon_list)
elif choice == 5:
break
else:
print('输入有误,请重新输入!')
```
这样,代码就更加清晰和易于维护了。
阅读全文