gpt4 book ai didi

python - 访问列表/从列表传递给类的数据

转载 作者:行者123 更新时间:2023-12-04 09:01:11 24 4
gpt4 key购买 nike

我最近正在尝试修改我正在为我的第一个程序编写的程序。如果每次游戏运行时用户被告知输入一些值,我就会让一切正常。
但是,我认为它应该具有我的程序/游戏每次都会使用的默认字符集。我的第一个目标是让每个玩家都拥有相同的默认 3 个字符。但最终这需要有一个至少包含字符名称的列表。
我的麻烦在于,如果他们说他们有新角色要创建,我每次都会收集数据,并尝试根据我的知识对其进行修改以使用列表并让它使用中的位置循环以获取该列表编号。
然后沿着我在类里面创建的相同路径传递该数据。但它似乎只是跳过这一部分,现在我不明白为什么(我确定这是一个缺乏经验的时刻)或者我试图错误地访问列表?无论如何,这方面的帮助将是巨大的,我在下面包含了我的代码。
我添加了注释行以指示似乎正在跳过的代码部分。
我希望对这个问题的澄清是 for in 循环是解决这个问题的正确方法吗?如果是这样,我是否以正确的方式访问它?

print ("Welcome to Chose your own adventure python edition")
print ("")


players = []

playerCharacters = []

def playerNames():
playerNum = int(input("How many players are playing? "))
if playerNum > 4:
print("I am sorry, unfortunately only four players are permitted.")

return


for playerId in range(playerNum):

newPlayerName = input(f"What is player {playerId + 1}'s name?")
players.append(newPlayerName)

print(f"Welcome: {' & '.join(players)}!")


def characters():
charAmount = 3

for index, player in enumerate(players):

playerCreate = input("{} (player {}), do you have a character to create. (y/n)".format(
player, str(index+1)))
if playerCreate.lower() =="y":
charAmount = int(input("How many characters does this player begin the game with?"))

for x in range(0,(charAmount)):
getCharName = input("Enter Next Char name ")
getCharDice = input("Please enter the number of dice this char will use. ")
getCharRole = input("Please enter the villagers role. ")

charData = {
"name": getCharName,
"diceCount": getCharDice,
"role": getCharRole,
"playerName": player
}

newCharacter = Character(characterData=charData)
newCharacter.printSummary()
playerCharacters.append(newCharacter)


if playerCreate.lower() == "n":
defaultCapture = input("Would you like to begin with the default charatures. (y/n)?" )

if defaultCapture.lower() == "y":

###Beginning of skipped code
for x in range (0,3):

DefaultCharName = ["Bob", "Sally", "Tommy"]
DefaultDiceCount = 1
DefaultRole = ['Builder', "Recruiter" , "Nothing"]



charData = {
"name": DefaultCharName(x),
"diceCount": DefaultDiceCount,
"role": DefaultRole(x),
"playerName": player
}

DefaultCharacters = Character(characterData=charData)
DefaultCharacters.printSummary()
playerCharacters.append(DefaultCharacters)
###End of skipped section

if defaultCapture.lower == "n":
print("Well it looks as though you dont really want to play.")
continue

print("Summary ==========================")
for player in playerCharacters:
print("{characterName} Controlled by {playerName}".format(
playerName=player.playerName,
characterName=player.name ))
return

class Character:

name = "default name"
playerName = "john/jane doe"
diceCount = "1"
role = "vanillaPaste"

def __init__(self, characterData):

self.playerName = characterData['playerName']
self.role = characterData['role']
self.diceCount = characterData['diceCount']
self.name = characterData['name']

def printSummary(self):
print("{player} summary: \r\n \r\nCharacters:\r\nName: {characterName} \r\nDice: {dice} \r\nRole: {role} \r\n"
.format(
characterName=self.name,
player=self.playerName,
dice=self.diceCount,
role=self.role
);


playerNames()
characters()

最佳答案

虽然您的代码就像评论所说的那样,对于最小的 SO 问题来说有点太长了,但在这种情况下,这是一个简单的解决方案。
就在“跳过部分的开始”之前,您有

if defaultCapture.lower == "y":
缺少括号来实际调用 .lower() , 小写字符串。 (将函数与字符串进行比较总是错误的。)
那个表情应该是
if defaultCapture.lower() == "y":

关于python - 访问列表/从列表传递给类的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63555653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com