因此,当我学习python时,我正在从事一个编码项目。这是一个相对简单的选择你自己的冒险游戏。我认为我把所有的东西都布置好了,但是当用户为var choice2选择“游泳”时,它应该给游戏带来消息并终止。然而,当我运行它,并选择“游泳”为choice2,它会给游戏的消息,但随后转移到choice11输入。我不知道自己到底做错了什么。代码如下:
print("Welcome to Treasure Island.")
print("Your mission is to find the treasure.")
choice1 = input("You\'re at a crossroad. There are three paths here. Do you want to go west, east, or south?").lower()
if choice1 == "west":
choice2 = input("You have entered a forest clearing, with a lake in the middle. Do you wait for the ferry, "
"swim across, or go back east?").lower()
if choice2 == "wait":
choice3 = input("You board the ferry and it takes you across the lake. "
"You see a house. Do you enter or return across the lake?").lower()
if choice3 == "enter":
print("It is dark inside the house. Suddenly you hear a growl as the light flares. "
"You are eaten by a grue. Game Over.")
else:
input(choice2)
elif choice2 == "swim":
print("You attempt to swim across, but suddenly you are attacked by a lake monster. Game Over.")
elif choice2 == "east":
input(choice1)
if choice1 == "east":
print("You come to a dead end in the forest. You are struck by several arrows from nowhere. Game Over.")
else:
choice11 = input("You move down the path and come to a fork in the road. Do you continue south or go east?").lower()
if choice11 == "south":
print("You come upon a treasure chest. When you open it, it is a mimic, with sharp nasty teeth. Game Over.")
else:
print("You come upon a veritable treasure hoard! You win!")发布于 2022-03-18 18:07:20
我怀疑这是:
if choice1 == "east":应该是这样:
elif choice1 == "east":当前代码中发生的情况是,整个第一个if choice1 == "west"块是独立的。因此,在"choice2“逻辑完成后,整个if块就会退出。然后,代码转到下一个if块,即if choice1 == "east"。该条件是false,因此控件进入else块。
通过使第二个顶级if成为一个elif,您可以将所有三个顶级块放入相同的整体条件计算中。
发布于 2022-03-18 18:23:03
print("Welcome to Treasure Island.")
print("Your mission is to find the treasure.")
choice1 = input("You\'re at a crossroad. There are three paths here. Do you want to go west, east, or south?").lower()
if choice1 == "west":
choice2 = input("You have entered a forest clearing, with a lake in the middle. Do you wait for the ferry, "
"swim across, or go back east?").lower()
if choice2 == "wait":
choice3 = input("You board the ferry and it takes you across the lake. "
"You see a house. Do you enter or return across the lake?").lower()
if choice3 == "enter":
print("It is dark inside the house. Suddenly you hear a growl as the light flares. "
"You are eaten by a grue. Game Over.")
else:
input(choice2)
elif choice2 == "swim":
print("You attempt to swim across, but suddenly you are attacked by a lake monster. Game Over.")
elif choice2 == "east":
input(choice1)
elif choice1 == "east":
print("You come to a dead end in the forest. You are struck by several arrows from nowhere. Game Over.")
else:
choice11 = input("You move down the path and come to a fork in the road. Do you continue south or go east?").lower()
if choice11 == "south":
print("You come upon a treasure chest. When you open it, it is a mimic, with sharp nasty teeth. Game Over.")
else:
print("You come upon a veritable treasure hoard! You win!")发布于 2022-03-18 18:28:06
你基本上是对的。为了结束程序,您需要使用exit() (它将提示用户关闭程序),或者使用带有函数的return 0。这将由returning False自动结束程序。以下是你如何做到的:
def adventure():
print("Welcome to Treasure Island.")
print("Your mission is to find the treasure.")
choice1 = input("You\'re at a crossroad. There are three paths here. Do you want to go west, east, or south?").lower()
if choice1 == "west":
choice2 = input("You have entered a forest clearing, with a lake in the middle. Do you wait for the ferry, "
"swim across, or go back east?").lower()
if choice2 == "wait":
choice3 = input("You board the ferry and it takes you across the lake. "
"You see a house. Do you enter or return across the lake?").lower()
if choice3 == "enter":
print("It is dark inside the house. Suddenly you hear a growl as the light flares. "
"You are eaten by a grue. Game Over.")
return 0 # Here you need to use it
else:
input(choice2)
elif choice2 == "swim":
print("You attempt to swim across, but suddenly you are attacked by a lake monster. Game Over.")
return 0 # Here you need to use it
elif choice2 == "east":
input(choice1)
elif choice1 == "east": # Also, this should be an elif as you already have an if statement i.e. if choice1 == "west"
print("You come to a dead end in the forest. You are struck by several arrows from nowhere. Game Over.")
return 0 # Here you need to use it
else:
choice11 = input("You move down the path and come to a fork in the road. Do you continue south or go east?").lower()
if choice11 == "south":
print("You come upon a treasure chest. When you open it, it is a mimic, with sharp nasty teeth. Game Over.")
return 0 # Here you need to use it
else:
print("You come upon a veritable treasure hoard! You win!")
adventure()输出:
Welcome to Treasure Island.
Your mission is to find the treasure.
You're at a crossroad. There are three paths here. Do you want to go west, east, or south?west
You have entered a forest clearing, with a lake in the middle. Do you wait for the ferry, swim across, or go back east?swim
You attempt to swim across, but suddenly you are attacked by a lake monster. Game Over.您所做的大多数事情都是正确的,但困扰我的一件事是,您在某些行中使用了input(choice1)或input(choice2)。我想你想要的是再次接受这个输入,对吗?再考虑一次,因为这样做根本行不通。
https://stackoverflow.com/questions/71531347
复制相似问题