我想创建一个函数,其中包含不同类型的敌人。但我不知道敌人的数量,他们会随机产生。然后,我想通过输入语句将它们打印出来,第一个敌人按1,第二个敌人按2,等等。所以这就像,你有3个僵尸,15个鬼魂,等等,但是,我希望它的创建方式可以让我以后添加更多输入,以便用户可以修改它,比如“你想从哪种类型的敌人中添加/删除敌人?”?。我需要你帮我做这件事。这是我到目前为止得到的,如果我的代码可以修改得更简单,更有意义,在某种程度上我可以学习和理解它,那将是完美的,谢谢
import random
def fillEnemyField():
print("Here's your first EnemyField")
zombies = []
zombies = random.randrange(0, 15)
trolls = []
trolls = random.randrange(0, 15)
vampires = []
vampires = random.randrange(0, 15)
ghosts = []
ghosts = random.randrange(0, 15)
def printEnemyField():
choice1 = int(input("Press 1 to get a new EnemyField"))
choice2 = int(input("Press 2 to get a new EnemyField"))
choice3 = int(input("Press 3 to get a new EnemyField"))
choice4 = int(input("Press 4 to get a new EnemyField"))
choice5 = int(input("Press 5 to quit"))
while choice != 5:
if choice ==1:
choice = print(fillEnemyField(zombies))
..............................
试试这个
您的函数没有返回任何内容,您可以返回一个字典,然后从中选择所需的值,例如:
另外,确保在while循环中更新choice,否则您将永远陷入循环中
或
可能更好的选择是,您可以将选项作为参数传入,然后返回该选项,例如:
相关问题 更多 >
编程相关推荐