0

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

好的,所以手头的问题是:

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

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

我希望它要求玩家“选择比赛”。在玩家输入他们想玩的内容后,它会询问“你确定吗?” |这就是我卡住的地方| 如果玩家说“是”,它会结束程序,我可以尝试继续构建这个应用程序。如果玩家输入“否”,它不会返回到原始问题并允许他们再次回答。

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

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!

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

4

2 回答 2

0

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

于 2019-11-12T19:38:39.763 回答
0

你要做的是建立一个菜单,用户可以在其中做出选择,并根据需要经常确认和重试他们的选择。

做到这一点的方法是使用无限的while循环。本质上,您只想不断重复相同的选项集,直到用户做出选择或退出。

此外,尽量让您的代码对用户所做的特定选择不感兴趣。我们关心他们从列表中选择一个项目;我们不需要关心哪个项目,只要我们在必要时有某种方法可以检索它。在下面的代码中,请注意除了原始列表之外,没有明确提及各个种族。

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  
于 2019-11-12T19:35:16.920 回答