python如何重命名lis

2024-04-27 23:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我目前正在为一个学校项目做一个战斗模拟,在这个项目中,两辆坦克将互相战斗。 每辆坦克都有可能拥有多个枪械。 所以我做了一个让用户创建枪支的方法,但是我想确保每次调用这个方法时,生成的列表都会得到保存在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

Tags: ofthe项目方法nameinputyourrange
1条回答
网友
1楼 · 发布于 2024-04-27 23:21:50

像这样做

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)

相关问题 更多 >