我在过去的3个月里一直是一名蟒蛇学生,而且我正在努力完成具有以下要求的作业。
您必须创建一个包含4个级别的多文本冒险。
每个选择必须在下一级别中产生3个独特的选择。
这意味着作业需要 3级选择,级别2中的9个选项,级别3中的27个选项和 4级中有81个选择。作为python的新手,我发现它非常粗糙,使其既高效,有条理,又能正常工作。
以下是我到目前为止的一个例子:
while adventure_running: # the entire branch of choices.. there might be an easier way to do this but I couldn't figure it out
print_situation('1')
answer = input().upper()
if answer == 'A':
print_situation('2_A')
answer = input().upper()
if answer == 'A':
print_situation('3_A1')
answer = input().upper()
if answer == 'A':
print_situation('4_A1_A1')
answer = input().upper()
if answer == 'A':
print("FINAL STATEMENT")
adventure_running = False
answer = 'D' # I set the answer value to D at the end so the program doesn't save the value throughout multiple runs
适当重复上述代码以适应所有120种选择。
print_situation函数查看字典并查找与其关联的提示和选项。
我的主要问题是,如果用户输入了错误的输入,它会重新启动整个文本冒险,而不是再次打印当前情况。
我知道这可能是非常低效的,所以我很乐意听到任何改进建议以及上述问题。
如果没有面向对象的编程,有没有办法做到这一点?
答案 0 :(得分:1)
您可以使用状态机:
state = '1'
table = {'1':{'A':'2_A'}, '2_A':{'A':'3_A1'},
'3_A1':{'A':'4_A1_A1'}, '4_A1_A1':{'A':'done'}}
while True:
print_situation(state)
if state == 'done':
break
state = table[state][input().upper()]
打印情况。检查我们是否处于完成状态。否则通过查找表进行转换。
请注意,如果你有话:
1级中有3个选项,2级中有9个选项,3级中有27个选项,第4级中有81个选项。
你有:
>>> 1+3+3*9+3*9*27+3*9*27*81
59809
的状态。
答案 1 :(得分:0)
这正是面向对象编程的原因。你需要选择,答案和Level的其他课程。然后构建对象。