python如何重命名列表

时间:2018-09-13 08:04:50

标签: python list dictionary object

因此,我目前正在为一个学校项目研究战斗模拟,其中两个战车将互相战斗。 每个坦克都必须有多个枪支物体。 因此,我制定了一种方法,让用户创建枪支,但我想确保每次调用此方法时,生成的列表都将保存在gunListName变量中。有没有办法做到这一点,因为我不知道如何做到这一点。

这是我正在谈论的方法:

 def createguns(self,):
    gunList = []
    gunListName = input("please input the name of the tank that will use these guns: ")
    gunNumber = input("how many guns do you wish to give your tank? ")
    for x in range(int(gunNumber)):
        gunName = input("please input the name of your gun: ")
        gunRange = input("please input the range of your gun in Meters(example: 1000 CM would be 1KM range): ")
        gunMinRange = input("please input the minimum range of your gun: ")
        gunDamage = input("please input the damage value of your tank: ")
        gunReload = input("please input the reload time of your gun")
        gunName = Gun(gunName,gunRange,gunMinRange,gunDamage,gunReload)
        gunList.append(gunName)
    return gunList

1 个答案:

答案 0 :(得分:0)

喜欢这样

def createguns(self):
    gunList = []
    gunListName = input("please input the name of the tank that will use these guns: ")
    gunNumber = input("how many guns do you wish to give your tank? ")
    for x in range(int(gunNumber)):
        gunName = input("please input the name of your gun: ")
        gunRange = input("please input the range of your gun in Meters(example: 1000 CM would be 1KM range): ")
        gunMinRange = input("please input the minimum range of your gun: ")
        gunDamage = input("please input the damage value of your tank: ")
        gunReload = input("please input the reload time of your gun")
        gunName = Gun(gunName,gunRange,gunMinRange,gunDamage,gunReload)
        gunList.append(gunName)
    return gunList, gunListName

gunList,name = createguns()
print(name)