我正在尝试使用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")
注意:我使用了一个由两个变量(位置和位置)组成的旧旅行系统,它工作正常,但效率不高,所以我决定改变旅行系统
为什么会改变
如果你这样做
你不希望
a
是5
要更新locationstr的值,需要使用
locationstr = ...
更新它我建议您通过函数更新locationstr。 (here is a guide for function in python if you need it)
所以你会这样做
当您想更改位置时,请这样称呼:
相关问题 更多 >
编程相关推荐