我创建了一个基于文本的冒险游戏,现在我正在研究玩家可以去的位置,我希望它在输入世界地图中的一个位置后返回到世界地图 旁注有谁知道如何使列表显示没有 ['',''] 这些标记?
我尝试在网上查找但没有帮助。我尝试了 ChatGPT 没有帮助。这是到目前为止我的代码:
import time
import random
class player:
def __init__(self):
self.name = playername
worldMap = ['Lake Placid', 'Cameeon']
cameeonMap = ['The Travelers Tavern', 'Simons Slave Shop', 'Hermiones House of Pleasure', 'Hermiones Health Spa', 'Alexs Armory', 'Wandas Weapons' ]
print("Welcome to Mizzorra this is a land of magic and monsters")
print("You have come here in search of treasure and the call of adventure")
print("What is thy name")
playername = input(" ")
print("You are a humble adventurer named " + playername )
print("You are starting out on a glory filled adventure" + playername + " Now you have a choice to make")
#This next part shoul create a loop where everytime you exit a place you choose where to go next in the map
while True:
print("Where do you go")
print(worldMap)
while True:
if input() == 'Cameeon' or 'cameeon':
print("You chose Cameeon, where in Cameeon would you like to go")
print(list(cameeonMap))
我建议阅读这篇文章,因为它解释了你所犯的错误,尽管是用 C++ 而不是 Python。
请记住,Python 不是英语。
if input() == 'Cameeon' or 'cameeon':
...
此代码相当于:
if (input() == 'Cameeon') or 'cameeon':
...
因此,在小写
'cameoon'
的情况下,您不会检查input()
是否等于它;您正在检查它的计算结果是否为 True
。由于它是一个非空字符串,因此它的计算结果始终为 True
。因此,无论用户输入什么,代码块都会执行,这可能不是您想要实现的目标。
旁注有人知道如何使列表显示没有 ['',''] 这些标记吗?
我相信你指的是这一行:
print(list(cameeonMap))
Python 中的 list() 函数从非列表的内容中创建一个列表。您将其与参数
cameeonMap
一起使用,该参数是在程序顶部定义的列表,因此本质上什么也不做。如果您想单独打印列表中的每个元素,请使用 join()
字符串方法。
print(" | ".join(cameeonMap))
这只是一个示例,将打印出由字符串
cameeonMap
分隔的 " | "
的每个元素。
>>> cameeonMap = ['The Travelers Tavern', 'Simons Slave Shop', 'Hermiones House of Pleasure', 'Hermiones Health Spa', 'Alexs Armory', 'Wandas Weapons' ]
>>> print(" | ".join(cameeonMap))
'The Travelers Tavern | Simons Slave Shop | Hermiones House of Pleasure | Hermiones Health Spa | Alexs Armory | Wandas Weapons'
正如 Elerium115 提到的,在开始询问具体问题之前,请阅读 Python 文档和/或阅读更多教程。