Python不能基于两个整数的值为字符串赋值

2024-05-14 11:12:05 发布

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

我正在尝试使用Python制作一个基于文本的游戏,并尝试在游戏中添加一个旅行系统

我在旅行中使用了三个变量 位置的两个整数(a和b) 和一个字符串来告诉玩家位置(locationstr)

代码通过输入为a和b赋值,并根据a和b的值为locationstr赋值,但即使a和b更改locationstr,也不会

这是密码

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

注意:我使用了一个由两个变量(位置和位置)组成的旧旅行系统,它工作正常,但效率不高,所以我决定改变旅行系统


Tags: thetodebugyouinputifis系统
1条回答
网友
1楼 · 发布于 2024-05-14 11:12:05

为什么会改变

如果你这样做

a = 4
b = a
b = 5

你不希望a5

要更新locationstr的值,需要使用locationstr = ...更新它

我建议您通过函数更新locationstr。 (here is a guide for function in python if you need it

所以你会这样做

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

当您想更改位置时,请这样称呼:

locationstr = change_loc(a, b)

相关问题 更多 >

    热门问题