如何将值存储在函数中并从另一个函数打印/修改?

2024-09-30 22:11:53 发布

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

我想创建一个函数,其中包含不同类型的敌人。但我不知道敌人的数量,他们会随机产生。然后,我想通过输入语句将它们打印出来,第一个敌人按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))
    ..............................

Tags: to类型newinputgetdefrandomint
2条回答

试试这个

import random

def fillEnemyField():
      # Define enemy amounts
      zombies = random.randrange(0, 15)
      trolls = random.randrange(0, 15)
      vampires = random.randrange(0, 15)
      ghosts = random.randrange(0, 15)

      # return enemy amounts so you can use them later
      # if you don't care about these names, you could do something like this
      # return [random.randrange(0, 15), random.randrange(0, 15), random.randrange(0, 15), random.randrange(0, 15]
      return [zombies, trolls, vampires, ghosts]

def printEnemyField():
    # when you do something multiple times, it's cleaner to use a loop
    for x in range(4):
        # this is formating a string, here is a great guide on this
        # https://www.digitalocean.com/community/tutorials/how-to-us-string-formatters-in-python-3
        print("Press {} to get a new EnemyField".format(x+1))
    print("Press 5 to quit") # this print is differnt, so we won't use it in the loop

    fields = fillEnemyField() # get all those enemies we worked so hard to make

    choice = int(input()) # get input from the user
    while choice != 5: # we want to quit on 5
        print(fields[choice-1]) # here we print out the ith value of the array, remember to subract 1 because arrays start at 0
        choice = int(input()) # get more input from the user so our loop doesn't just print forever

printEnemyField() # remember to call your functions from somewhere, otherwise it wont get run

您的函数没有返回任何内容,您可以返回一个字典,然后从中选择所需的值,例如:

另外,确保在while循环中更新choice,否则您将永远陷入循环中

import random
def fillEnemyField():
    print("Here's your first EnemyField")
    return {"zombies": random.randrange(0, 15),
            "trolls": random.randrange(0, 15),
            "vampires": random.randrange(0, 15),
            "ghosts": random.randrange(0, 15)
            }

def printEnemyField():
    print("Press 1 to get a new EnemyField")
    print("Press 2 to get a new EnemyField")
    print("Press 3 to get a new EnemyField")
    print("Press 4 to get a new EnemyField")
    print("Press 5 to quit")
    choice = int(input("Enter a number: "))
    while choice != 5:
        if choice ==1:
            print(fillEnemyField()['zombies'], "zombies in the field")
        choice = int(input("Enter a number: "))

printEnemyField()

可能更好的选择是,您可以将选项作为参数传入,然后返回该选项,例如:

def fillEnemyField(choise):
    print("Here's your first EnemyField")
    options = {"zombies": random.randrange(0, 15),
               "trolls": random.randrange(0, 15),
               "vampires": random.randrange(0, 15),
               "ghosts": random.randrange(0, 15)
               }
    return options[choise]


def printEnemyField():
    ...
    choice = int(input("Enter a number: "))
    while choice != 5:
        if choice == 1:
            print(fillEnemyField('zombies'), "enemies on the field")
        choice = int(input("Enter a number: "))

相关问题 更多 >