Home > Software engineering >  Is there a way to execute a statement only once in python?
Is there a way to execute a statement only once in python?

Time:12-24

I am trying to make a RPG game using python but I am having trouble as I have to allow a user to receive a certain Item only once. If the user interacts with a NPC for the first time they should receive an item, if the user interacts with the same NPC again, they should receive a quote. Is there any way to prevent the NPC to give the user the same thing if they are interacted more than once?

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()

CodePudding user response:

Sure, there are multiple ways.

You could have a mutable function, then your code would look like this:

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()

Word of caution regarding mutable arguments: https://florimond.dev/en/posts/2018/08/python-mutable-defaults-are-the-source-of-all-evil/

Or you could have a global flag or array, something like:

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()

CodePudding user response:

If Sword is False than Player will receive a gift and record an entry that Sword is True (Present).In if function "and" Operator make sure that both conditions are True i.e. Sword is False.

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()

CodePudding user response:

You can also look at changing the entire implementation of function() as part of executing it via a closure. The advantage here might be that the method itself keeps track of the proper implementation rather than using a "global" variable.

## -------------------------------
## 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()
## -------------------------------

This will give you output that might look something like:

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


What do you want to do?
1. Talk to the blacksmith
2. Leave
1
Hi, how can I help?

CodePudding user response:

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
  • Related