因此,这只是我将要遇到的一长串问题的开始。在这个基于文本的冒险中,我希望最终遇到困惑和多个分支路径,最终可以加入的派系,影响情况道德的选择对话(例如,质量效应或科托尔,但是基于文本的ish)等等。 。,但我觉得早期的设置对于这次学习非常重要。我还希望最终将其转换为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!
我尝试了几种不同的方法来获得所需的结果,但是它们似乎没有用。我应该建立一个有限状态机吗?我该怎么做?延长寿命并使代码模块化以便将来更新的最佳方法是什么?
您试图做的是建立一个菜单,用户可以在其中进行选择,并根据需要确认并重试他们的选择。
实现此目的的方法是使用无限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
有一种非常简单的方法。您需要通过import time
导入“时间”模块或者您可以使用最简单的方法,在代码开头添加while True:
。 !!不要忘记“:”之后的空格!