为什么我的角色 sheet 在尝试在基于文本的冒险中选择种族时不能使用 input()? python3.x

Why doesn't my character sheet work with input() when trying to choose a race in a text based adventure? python3.x

所以这只是我知道我将要遇到的一长串问题的开始。在这个基于文本的冒险中,我希望最终有谜题和多条分支路径,你最终可以加入的派系,影响情境道德的选择对话(如质量效应或科托尔但..基于文本的)等等., 但我觉得早期设置对于这次学习之旅非常重要。我还想最终将其转换为 PYQT5,并可能最终在我为我的投资组合构建的网站上使用 UI 托管它。我只是想把它弄清楚,以防你经常在这里看到我。如果你愿意,请私信我(因为我绝对可以得到守护神的帮助!)。

好的,那么手头的问题是:

我有一个可供选择的种族列表:

races = ['Human', 'Dwarf', 'Elf', 'Dragonborn', 'Tiefling', 'Half-Elf']

我想请玩家'Choose a race'。 在玩家输入他们想玩的内容后,它会询问 'Are you sure?' |这是我被困的地方| 如果播放器说 'yes' 它会结束程序,我可以尝试继续构建此应用程序。 如果玩家输入 'no',它不会 return 回到原来的问题并让他们再次回答。

所以,我尝试过的东西被定义为不同的方法:

Character.charRace()
Character.charDict()
Character.charTry()

我认为我糟糕的代码尝试就我一直在尝试的事情而言不言而喻。

class Character:

    def charRace():
        raceOptions = ['Human', 'Dwarf', 'Elf', 'Dragonborn', 'Tiefling', 'Half-Elf']
        raceChoice = input(f'Choose a Race: {raceOptions} \n')

        if raceChoice == 'Human':
            res = input(
                """
                Choose Human? 
                (Y   /    N)
                """)

            if res == 'Y':
                print(f'You Chose {raceChoice}!')
            if res == 'N':
                del raceChoice
                raceChoice = input(f'Choose a Race: {raceOptions} \n')
                res = input(
                    """
                    Choose Human? 
                    (Y   /    N)
                    """)

        if raceChoice == 'Dwarf':
            res = input(
                """
                Choose Dwarf? 
                (Y    /    N)
                """)
            if res == 'Y':
                print(f'You Chose {raceChoice}!')
            if res == 'N':
                del raceChoice
                raceChoice = input(f'Choose a Race: {raceOptions} \n')

        if raceChoice == 'Elf':
            res = input(
                """
                Choose Elf? 
                (Y   /    N)
                """)

            if res == 'Y':
                print(f'You Chose {raceChoice}!')
            if res == 'N':
                del raceChoice
                raceChoice = input(f'Choose a Race: {raceOptions} \n')

        if raceChoice == 'Dragonborn':
            res = input(
            """
            Choose Dragonborn?
            (Y    /    N)
            """)

            if res == 'Y':
                print(f'You Chose {raceChoice}!')
            if res == 'N':
                del raceChoice
                raceChoice = input(f'Choose a Race: {raceOptions} \n')

        if raceChoice == 'Tiefling':
            res = input(
            """
            Choose Tiefling?
            (Y    /    N)
            """)

            if res == 'Y':
                print(f'You Chose {raceChoice}!')
            if res == 'N':
                del raceChoice
                raceChoice = input(f'Choose a Race: {raceOptions} \n')

        if raceChoice == 'Half-Elf':
            res = input(
            """
            Choose Half-Elf?
            (Y    /    N)
            """)

            if res == 'Y':
                print(f'You Chose {raceChoice}!')
            while res == 'N':
                del raceChoice
                raceChoice = input(f'Choose a Race: {raceOptions} \n')
                res = input(
                    """
                    Choose Half-Elf?
                    (Y    /    N)
                    """)

        if raceChoice == 'Orc':
            res = input(
                """
                Choose Orc?
                (Y    /    N)
                """)
            while res:
                if res == 'y':
                    print(f'You Chose {raceChoice}!')
                    break
                if res == 'n':
                    del raceChoice
                    raceChoice = input(f'Choose a Race: {raceOptions} \n')
                    res = input(
                        """
                        Choose Orc?
                        (Y    /    N)
                        """)

    def charDict():
        raceOptions = ['Human', 'Dwarf', 'Elf', 'Dragonborn', 'Tiefling', 'Half-Elf']
        raceChoice = input(f'Choose a Race: {raceOptions} \nRace: ')
        if raceChoice == 'Human' or 'human' or 'h':
            print(f'Choose {raceChoice}?\n')
            ans = input()
            if ans != 'Yes' or  'yes' or 'y':
                print(f'You chose {raceChoice}! ')
            elif ans == 'No' or 'no' or 'n':
                return raceChoice
            if raceChoice != 'Human' or 'human' or 'h' or 'Dwarf' or 'dwarf' or 'd' or 'Elf' or 'elf' or 'e' or 'Dragonborn' or 'dragonborn' or 'DB' or 'Tiefling' or 'tiefling' or 't':
                return 'That is not a race that can be chosen at this time!'

    def charTry():
        races = ['Human', 'Dwarf', 'Elf', 'Dragonborn', 'Tiefling', 'Half-Elf']
        res = input(f'Choose a Race: {races}. \nRace: ')
        race = res.capitalize()
        if race in races:
            if race == 'human' or 'Human' or 'h':
                print(f'Do you want to choose {race}? ')

    Character.charRace()
    Character.charDict()
    Character.charTry()

期望的结果示例:

>>> f'Choose a race {races}!:
>>> Human
>>> Do you want to play a Human?
>>> (Y    /    N)
>>> N
>>> Continue to browse...
>>> Choose a race: Human, Dwarf, Elf, Dragonborn, Tiefling, Half-Elf
>>> Dwarf
>>> Do want to play a Dwarf?
>>> Y
>>> You have chosen to play a Dwarf!

我尝试了几种不同的方法来获得想要的结果,但它们似乎都不起作用。 我应该建立一个有限状态机吗?我该怎么做?延长寿命并使代码模块化以供将来更新的最佳方法是什么?

您尝试做的是构建一个菜单,用户可以在其中做出选择并根据需要随时确认和重试他们的选择。

执行此操作的方法是使用无限循环。本质上,您只想不断重复同一组选项,直到用户做出 selection 或退出。

此外,尽量让您的代码对用户制作的特定 selection 无动于衷。我们关心他们 select 列表中的一项;我们不需要关心 which 项目,只要我们有办法在必要时检索它即可。在下面的代码中,请注意除了原始列表之外,没有明确提及个别种族。

class Character:                                                                                                                                                                                                                                                                                

    RACES = ['Human', 'Dwarf', 'Elf', 'Dragonborn', 'Tiefling', 'Half-Elf']                                                                                      

    def choose(self):
        # list choices as (0) Human, (1) Dwarf etc                                                                                                                                                                                                                                                                             
        choices = ', '.join([f'({i}) {race}' for i, race in enumerate(self.RACES)])    
        # Loop forever!                                                                          
        while True:                                                                                                                                              
            choice = input(f'Choose a race: {choices} ')                                                                                                         
            choice = choice.upper()                                                                                                                              
            if choice == 'Q':                                                                                                                                    
                print('Bye!')  
                # Break out of the loop                                                                                                                                  
                break                                                                                                                                            
            elif not(choice.isdigit()):                                                                                                                          
                # Not a number                                                                                                                                   
                print('Sorry, please choose a number')                                                                                                                       
            elif not 0 <= int(choice) <= len(self.RACES):                                                                                                        
                # Number outside the range of choices                                                                                                            
                print('Sorry, please choose again')                                                                                                                                                                                                                                        
            else:   
                # The number input by the user matches the position
                # of their choice in the list of races                                                                                                                                             
                selection = self.RACES[int(choice)]                                                                                                              
                confirm = input(f'You chose {selection} - confirm (Y/N)? ')                                                                                      
                if confirm.upper() == 'Y':                                                                                                                       
                    print(f'Confirmed - you\'re a {selection}!')
                    # Break out of the loop                                                                                                 
                    break                                                                                                                                        
                else:                                                                                                                                            
                    print('OK, let\'s try again')                                                                                                                                                                                                                                                   
        return  

有一种非常简单的方法可以做到这一点。您需要通过 import time 导入 'time' 模块 或者您可以使用最简单的方法,在代码开头添加 while True: 。 !!不要忘记“:”后的空格!然后在每个 if 语句之后,您需要像这样添加 'continue':if res == 'Y': print(f'You Chose {raceChoice}!') continue