访问列表/从列表传递到类的数据

2024-04-19 05:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近正试图对我正在为我的第一个程序编写的一个程序进行修改。如果每次游戏运行时都要求用户输入一些值,那么我的一切都正常工作

然而,我认为它应该有一组默认的角色,我的程序/游戏每次都会使用这些角色。我的第一个目标是给每个玩家相同的默认3个角色。但最终这将需要一个至少包含字符名的扩展列表

我的问题在于,我采用了一个if循环,每次当他们说他们有新字符要创建时,都会收集数据,并尝试根据我所知对其进行修改,以使用列表,并让它使用循环中的位置来获取列表编号

然后按照我在类中创建的相同路径传递数据。但它似乎只是跳过了这一部分,我现在不明白为什么(我确信这是一个没有经验的时刻),或者我试图访问列表错误?无论如何,这方面的帮助将是巨大的,我已经包括我的代码如下

我添加了注释行,以指示似乎要跳过的代码部分

我希望对这个问题进行澄清,这是解决这个问题的正确方法吗?如果是这样的话,我的访问方式是否正确

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()

Tags: tonameinself列表forinputif
2条回答

尝试检查for循环的缩进,输入内容时,不要有空格。你应该使用

 defaultCapture.lower().split =="y"

defaultCapture.lower().split.contains("y")// Add the brackets()

正如评论所说,虽然您的代码对于最小的SO问题来说有点太长,但在本例中,这是一个简单的修复方法

就在“跳过部分的开始”之前,您有

if defaultCapture.lower == "y":

它缺少实际调用.lower()的括号,以降低字符串的大小写。(将函数与字符串进行比较总是错误的。)

这个表达应该是

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

相关问题 更多 >