我正在用python开发一款基于文本的游戏。我目前停留在一个系统上,在该系统上您可以解锁某个功能,一旦解锁该功能,您必须获得一定数量的积分,您才能解锁下一个功能。但是我遇到了麻烦,这是我的代码:
if HS >= 100 and HS <= 199:
print("You have enough HS points to unlock a new feature!")
conf = input("Do you want to unlock a new feature? ")
if conf == "yes":
features.append("feature1")
locked_features.remove("feature1")
HS = HS - 100
print("You have unlocked feature1!")
print("Your available feature are: ", features)
print("You now have", HS, "HS points. ")
elif HS >= 200 and HS <= 299:
features.append("feature2")
locked_features.remove("feature2")
HS = HS - 100
print("You have unlocked feature2!")
print("Your available feature are: ", features)
print("You now have", HS, "HS points. ")
HS是解锁新功能所需的要点。有一个名为“功能”的列表,其中包含播放器的所有可用功能。锁定功能是玩家解锁之前无法使用的功能。 但是,当我解锁功能1并获得100 HS积分时,它会问我是否要解锁新功能。但是,当我键入“是”时,它只会出现错误“ Feature1不在Locked_features中”。如果我已经解锁了功能1,该如何跳过功能1的行?
答案 0 :(得分:0)
有几种方法可以做到这一点。一种可能是在if中添加其他条件。如果功能中没有“ feature1”,就这样。
对于第一个IF,您可以:
if HS >= 100 and HS <= 199 and "feature1" not in features:
答案 1 :(得分:0)
我不知道这是否正是您想要的逻辑,但这是基本思想...只需在“功能”列表中查看以查看该功能是否已启用:
if HS >= 100 and HS <= 199:
if not "feature1" in features:
print("You have enough HS points to unlock a new feature!")
conf = input("Do you want to unlock a new feature? ")
if conf == "yes":
features.append("feature1")
locked_features.remove("feature1")
HS = HS - 100
print("You have unlocked feature1!")
print("Your available feature are: ", features)
print("You now have", HS, "HS points. ")
if HS >= 100 and HS <= 199:
if not "feature2" in features:
features.append("feature2")
locked_features.remove("feature2")
HS = HS - 100
print("You have unlocked feature2!")
print("Your available feature are: ", features)
print("You now have", HS, "HS points. ")
我还更改了逻辑,以便如果您已经拥有第一个功能,则可以获得100 HS的第二个功能,如果您拥有200 HS,则仍可以同时获得两个功能。
答案 2 :(得分:0)
首先,在我看来,您在elif语句上出现了缩进错误。关于您的问题,我认为您可以每次仅检查功能是否存在于数组中。
用以下代码替换上面显示的代码的第四行:
if(conf == "yes" and "feature1" in locked_features):
这样,每次用户选择“是”时,该语句仅在您要删除的功能(即“ feature1”)中处于锁定状态时运行。但是我认为程序的总体流程是错误的。您询问用户通常是否要解锁新功能,并且当他/她选择“是”时,您会自动选择功能1。我不确定这就是您想要的。您应该分别检查用户是否要解锁功能,然后询问有关功能编号或其他内容的输入。只是一点提示:)
答案 3 :(得分:0)
我认为这就是您要寻找的。现在,if语句不再检查点,而是检查每个功能是否在locked_features
中。
if HS >= 100: # I removed the cap of 199.
print("You have enough HS points to unlock a new feature!")
conf = input("Do you want to unlock a new feature? ")
if conf == "yes":
if "feature1" in locked_features: # Added this check after conf == yes
features.append("feature1")
locked_features.remove("feature1")
HS = HS - 100
print("You have unlocked feature1!")
print("Your available feature are: ", features)
print("You now have", HS, "HS points. ")
elif "feature2" in locked_features: # Similarly for feature2, and so on
features.append("feature2")
locked_features.remove("feature2")
HS = HS - 100
print("You have unlocked feature2!")
print("Your available feature are: ", features)
print("You now have", HS, "HS points. ")