在基于文本的冒险中尝试选择种族时,为什么角色表不能与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

查看:104
本文介绍了在基于文本的冒险中尝试选择种族时,为什么角色表不能与input()一起使用? python3.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这仅仅是我知道我将要遇到的一长串问题的开始。在这个基于文本的冒险中,我希望最终有难题和多个分支路径,最终可以加入的派系,影响情况道德的选择对话(例如,质量效应或kotor,但基于文本的ish)等。,但我觉得早期的设置对于这次学习非常重要。我还希望最终将其转换为PYQT5,并最终通过用户界面在我为自己的投资组合建立的网站上托管它。我只是想消除这种情况,以防您在这里经常看到我。下午给我一些建议,如果您愿意的话(因为我绝对可以使用守护神的帮助!)。



好的,所以当前的问题是:



我有一系列比赛可供选择:

 种族= [人类,矮人,小精灵,龙婆,小偷,半精灵] 

我希望它让玩家选择比赛。
玩家输入想要玩的游戏后,它会问您确定吗?
|这是我被困的地方|
如果播放器说是,则程序结束,我可以尝试继续构建此应用程序。
如果玩家键入 no,它不会返回到原始问题,而是让他们再次回答。



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

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

我认为我的可怜代码尝试可以说明我自己的观点

  class字符:

def charRace():
raceOptions = [ '人类','矮人','精灵','龙婆','小偷','半精灵']
raceChoice = input(f'选择种族:{raceOptions} \n')

如果raceChoice =='Human':
res = input(

选择人类?
(是/否)


如果res =='Y':
print(f'You选择{raceChoice}!')
如果res =='N':$ b $种族竞赛oice
raceChoice =输入(例如,选择种族:{raceOptions} \n’)
res =输入(

选择人类?
(是/否)


如果raceChoice =='Dwarf':
res =输入(

选择矮人吗?
(是/否)

如果res =='Y':
print(f'You选择{raceChoice}!')
if res =='N':
del raceChoice
raceChoice = input(f'选择种族:{raceOptions} \n')

if raceChoice =='Elf':
res = input(

选择Elf吗?
(Y / N)


如果res =='Y':
print(f'You选择{raceChoice}!')
if res =='N':
del raceChoice
raceChoice = input(f'选择种族:{raceOptions} \n')

如果raceChoice =='Dragonborn':
res =输入(

选择Dragonborn?
(是/否)


如果res =='Y':
print(f'You选择{raceChoice}!')
if res =='N':
del raceChoice
raceChoice =输入(f'选择种族:{raceOptions} \n')

if raceChoice = ='Tiefling':
res = input(

选择Tiefling吗?
(Y / N)


如果res =='Y':
print(f'You选择{raceChoice}!')
if res =='N':
del raceChoice
raceChoice = input(f'选择种族:{raceOptions} \n')

if raceChoice =='Half-Elf':
res = input(

选择半精灵吗?
(是/否)


如果res =='Y':
print(f'You选择{raceChoice}!')
while res =='N':
del raceChoice
raceChoice =输入(f'选择种族:{raceOptions} \n')
res =输入(

选择半精灵?
(是/否)


if raceChoice =='Orc':
res =输入(

选择Orc?
(是/否)

而res:
如果res =='y':
print(f'You选择{raceChoice}!')如果res =='n'
打破

del raceChoice
raceChoice = input(f'选择种族:{raceOptions} \n ')
res =输入(

选择Orc?
(是/否)


def charDict():
raceOptions = ['人类','矮人','精灵','龙婆','铁夫林','半精灵']
raceChoice =输入(f'选择种族:{raceOptions} \nRace:')
如果raceChoice =='人类'或' human'或'h':
print(f'Choose {raceChoice}?\n')
ans = input()
如果ans!='Yes'或'yes'或'y':
print(f'您选择了{raceChoice}! ')
elif ans =='否'或'否'或'n':
return raceChoice
if raceChoice!='人类'或'人类'或'h'或'矮人'或'矮人'或'd'或'精灵'或'精灵'或'e'或'Dragonborn'或'dragonborn'或'DB'或'Tiefling'或'tiefling'或't':
return'那不是当前可以选择的种族!'

def charTry():
races = ['Human','Dwarf','Elf','Dragonborn ','Tiefling','Half-Elf']
res = input(f'选择种族:{races ... nRace:')
race = res.capitalize()
如果种族竞赛:
如果种族=='human'或'Human'或'h':
print(f'您要选择{race}?')

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



所需结果示例:



 >> f’选择种族{竞赛} !: 
>>>人
>>你想扮演人类吗?
>> (是/否)
>> N
>>继续浏览...
>>>选择种族:人类,矮人,精灵,龙之谷,铁夫林,半精灵
>>矮人
>>是否想扮演小矮人?
>> Y
>>您选择扮演小矮人!

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

解决方案

您要尝试做的是构建菜单,用户可以根据需要进行选择并确认并重试选择。



执行此操作的方法是无限的while循环。本质上,您只想继续重复相同的选项集,直到用户做出选择或退出为止。



此外,请尝试使代码对用户的特定选择无动于衷。我们关心他们从列表中选择一个项目;只要我们有必要在必要时对其进行检索,我们就不必关心哪个项目。在以下代码中,请注意,除了原始列表外,没有明确提及各个种族。

 类字符:

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

def选择(自己):
#列出以下选项:(0)人,(1)矮人等
choices =','.join([f'({i}){race}'for i,in enumerate(self.RACES)])
#永远循环!
而True:
选择=输入(f'选择种族:{choices}')
choice = choice.upper()
if choice =='Q':
print('Bye!')
#打破循环
休息
elif not(choice.isdigit()):
#不是数字
print('对不起,请选择一个数字')
elif not 0< = int(choice )< = len(self.RACES):
#数字超出选择范围
打印(抱歉,请再次选择)
else:
#用户输入的数字与他们在种族列表中选择的位置
#相匹配
selection = self.RACES [int(choice)]
确认=输入(f'您选择了{选择}-确认(是/否)? ')
if confirm.upper()=='Y':
print(f'已确认-您是{选择}!')
#退出循环
休息
其他:
打印(好,让我们再试一次)
返回


So this is just the beginning of a long line of questions I know that I am going to have. In this text based adventure I would like to eventually have puzzles and multiple branching paths, factions you can eventually join, choice dialogue that affects the morality of situations(like mass effect or kotor but.. text based-ish), etc., etc., but I feel like the early set up is VERY important for this learning journey. I also would like to eventually convert it over to PYQT5 and maybe eventually host it with a UI on a website I've built for my portfolio. I just wanted to get that out of the way in case you see me on here a lot. PM me with advice for that if you'd like(Because I definitely could use the help from a patron saint!).

Okay, so the problem at hand is:

I have a list of races to choose from:

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

I would like for it to ask the player to 'Choose a race'. After the player types what they want to play it asks 'Are you sure?' |Here is where I am stuck| If the player says 'yes' it ends the program and I can attempt to continue building this application. If the player types 'no', it doesn't return back to the original question and allow them to answer it again.

So, the things I have tried are defined as different methods:

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

I think my poor code attempts speak for themselves in terms of what I've been trying.

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

Desired Result Example:

>>> 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!

I've tried a few different methods to acquire the desired result but they don't seem to work. Should I build a finite state machine? How would I do that? What is the best method for longevity and making the code modular for future update?

解决方案

What you are try to do is to build a menu, where the user may make choices and confirm and retry their choices as often as necessary.

The way to do this is with an infinite while loop. Essentially, you just want to keep repeating the same set of options until the user makes a selection, or quits.

Also, try to keep your code indifferent to the particular selection that the user makes. We care that they select an item from the list; we don't need to care which item, as long as we have some way of retrieving it when necessary. In the following code, note that there is no explicit mention of the individual races except in the original list.

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  

这篇关于在基于文本的冒险中尝试选择种族时,为什么角色表不能与input()一起使用? python3.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆