在Python中,是否有办法让一个语句只执行一次?

1 人关注

我正在尝试用python制作一个RPG游戏,但我遇到了麻烦,因为我必须允许用户只收到某个物品一次。如果用户第一次与一个NPC互动,他们应该收到一个物品,如果用户再次与同一个NPC互动,他们应该收到一个报价。 有什么办法可以防止NPC在与用户互动超过一次的情况下给用户同样的东西?

def function():
  print("What do you want to do?")
  userInput = input("1. Talk to the blacksmith \n2. Leave\n")
  if userInput == "1":
      # if the user hasnt already received the sword:
      print("the BLACKSMITH gifts you a sword")
    else:
      print("Hi, how can I help?")
  function()
function()
    
1 个评论
在你的用户类中使用一个保存互动的属性?
python
python-3.x
loops
Cavio
Cavio
发布于 2021-12-23
4 个回答
alexisdevarennes
alexisdevarennes
发布于 2021-12-24
已采纳
0 人赞同

当然,有多种方法。

你可以有一个可变函数,那么你的代码就会是这样的。

def function(given_items=[]):
  print("What do you want to do?")
  userInput = input("1. Talk to the blacksmith \n2. Leave\n")
  if userInput == "1":
      # if the user hasnt already received the sword:
      if not "sword" in given_items:
          print("the BLACKSMITH gifts you a sword")
          given_items.append("sword")
    else:
      print("Hi, how can I help?")
  function()
function()

关于易变的论据,需要注意的是。https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/

或者你可以有一个全局标志或数组,类似的东西。

GIVEN_ITEMS = []
# SWORD_GIVEN = FALSE # You could also use a boolean instead of a list, the advantage with a list is that it allows you to keep track of all such items instead of having a boolean flag for every single item.
def function():
  print("What do you want to do?")
  userInput = input("1. Talk to the blacksmith \n2. Leave\n")
  if userInput == "1":
      # if the user hasnt already received the sword:
      if not "sword" in GIVEN_ITEMS:
          print("the BLACKSMITH gifts you a sword")
          GIVEN_ITEMS.append("sword")
    else:
      print("Hi, how can I help?")
  function()
function()
    
非常感谢你!它的工作原理:)
@Cavio 如果是这样,请考虑将答案标记为正确,以便其他人更容易找到它。
Mohammad Zohaib
Mohammad Zohaib
发布于 2021-12-24
0 人赞同

如果Sword是假的,那么玩家将收到一份礼物,并记录Sword是真(Present)的条目。在if函数 "和 "操作中,确保两个条件都是真,即Sword是假。

Sword = False
def function():
  print("What do you want to do?")
  userInput = input("1. Talk to the blacksmith \n2. Leave\n")
  if userInput == "1" and Sword == False:
      # if the user hasnt already received the sword:
      print("the BLACKSMITH gifts you a sword")
      Sword = True:
    else:
      print("Hi, how can I help?")
  function()
function()
    
Loop_Assembly
Loop_Assembly
发布于 2021-12-24
0 人赞同

There are many ways to achieve this:

You can have a variable outside your function like:
Temp = True
And Make a variable change in your function something similar to this :
global Temp
Temp = False
So the actual code would be :
Temp = True
def function():
  print("What do you want to do?")
  userInput = input("1. Talk to the blacksmith \n2. Leave\n")
  if userInput == "1":
      # if the user hasnt already received the sword:
      print("the BLACKSMITH gifts you a sword")
  else:
      print("Hi, how can I help?")
  global Temp
  Temp = False
while True:
    if Temp != False:
        function()
    else:
        print(Temp)
        break
Now the while would be looking for the variable change and will perform the loop iteration accordingly
JonSG
JonSG
发布于 2021-12-24
0 人赞同

我可能会用一个封闭式的。

你也可以考虑改变 function() 的整个实现,作为通过闭包执行它的一部分。这样做的好处是,方法本身可以跟踪正确的实现,而不是使用一个 "全局 "变量。

## -------------------------------
## A closure that encapsulates some internal methods and when called
## returns one of them that can be assigned to a more global variable
## that can then be called.
## -------------------------------
def build_blacksmith_conversation():
    ## -------------------------------
    ## The implementation we want if this is the first time
    ## -------------------------------
    def first_time():
        nonlocal active    ## this variable is defined in the outer context
        print("What do you want to do?")
        userInput = input("1. Talk to the blacksmith \n2. Leave\n")
        if userInput == "1":
            print("the BLACKSMITH gifts you a sword\n\n")
            ## -------------------------------
            ## since you have the sword, reset the active implementation
            ## -------------------------------
            active = not_first_time
            ## -------------------------------
    ## -------------------------------
    ## -------------------------------
    ## The implementation we want if this is not first time
    ## -------------------------------
    def not_first_time():
        print("What do you want to do?")
        userInput = input("1. Talk to the blacksmith \n2. Leave\n")
        if userInput == "1":
            print("Hi, how can I help?\n\n")
    ## -------------------------------
    ## -------------------------------
    ## Set the active implementation
    ## -------------------------------
    active = first_time
    ## -------------------------------
    ## -------------------------------
    ## return a method that when called calls whatever the "active" method is
    ## -------------------------------
    return lambda : active()
    ## -------------------------------
## -------------------------------
## -------------------------------
## Create a new method blacksmith_conversation() that will initially use
## one implementation but after the sword is given will switch to a new one
## -------------------------------
blacksmith_conversation = build_blacksmith_conversation()
## -------------------------------
## -------------------------------
## Give the sword
## -------------------------------
blacksmith_conversation()
## -------------------------------
## -------------------------------
## offer help
## -------------------------------
blacksmith_conversation()
## -------------------------------

这将给你提供可能看起来像这样的输出。

What do you want to do?
1. Talk to the blacksmith 
2. Leave
the BLACKSMITH gifts you a sword
What do you want to do?