所以这只是我知道我将会遇到的一长串问题的开始。在这个基于文本的冒险中,我希望最终有谜题和多个分支路径,你最终可以加入的派系,影响情况道德的选择对话(如质量效应或科托尔,但是..基于文本的)等等,等等.,但我觉得早期设置对于这次学习之旅非常重要。我还希望最终将其转换为 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!
我尝试了几种不同的方法来获得所需的结果,但它们似乎不起作用。我应该建立一个有限状态机吗?我该怎么做?长寿和使代码模块化以供将来更新的最佳方法是什么?