Python can't assign value to a string based on values of two integers

Viewed 323

I'm trying to make a text based game using Python and I'm trying to add a travel system to the game

I'm using three variables for travel two integers for locations (a & b) and a string to tell the player the location(locationstr)

the code assigns values to a and b by input and supposed to give a value to locationstr based on values of a and b but even though a and b changes locationstr does not

here is the code

#Variables
#new travel system variables
a = 3
b = 3
#New Location definitions
if (a == 3 and b == 3):
 locationstr = "the Center of the Wilds"
elif (a == 2 and b == 2):
 locationstr = "the Northwestern plains of the Wilds"
#Game
print("You are currently in", locationstr)
print("Debug: a", a)
print("Debug: b", b)
while True:
 print("Do you want to travel somewhere?")
 print("(T)ravel")
 print("Do you want to end the day?")
 print("(Y)es")
 print("(N)o")
 #Choice input
 x = input("What is your choice?")
 if (x == "Y" or x == "y"):
     #Ending the day
  print("You decided to end the day")

  print("###########################################################################")
  print("You are currently in", locationstr)
  print("Debug: a", a)
  print("Debug: b", b)
 elif (x == "N" or x == "n"):
  print("###############################################################################")
  print("You decided to not end the day")
 elif (x == "T" or x == "t"):
     #Travel system
      print("###########################################################################")
      print("You decided to travel somewhere")
      print("Where to travel?")
      print("(1) Northwest")
      ti = input("What is your choice?")
      if(ti == "1"):
       a = a - 1
       b = b -1
       print("########################################################################")
       print("You traveled to", locationstr)
       print("Debug: a", a)
       print("Debug: b", b)
      else:
         print("#######################################################################")
         print(ti, "is not a valid choice")
 else:
  print(x, "is not a valid choice")

Note: I used an old travel system by two variables(location and locatşonstr) it was working but it wasn't efficient so i decided to change the travel system

1 Answers

Why would it change?

If you do

a = 4
b = a
b = 5

You don't expect a to be 5

To update the value of locationstr you need to update it with locationstr = ...

I advise you to update locationstr via a function. (here is a guide for function in python if you need it)

So you would do something like

def change_loc(a, b):
   if (a == 1 and b == 1):
      return "the Northwestern corner of the Wilds"
   ...

and when you want to change location call it like this:

locationstr = change_loc(a, b)
Related