【问题标题】:Python Text Adventure LoopPython 文本冒险循环
【发布时间】:2014-02-22 22:06:15
【问题描述】:

我正在 python 3.3.4 中制作一个非常简单的文本冒险游戏,并且想知道当输入与 a/b 不匹配时是否有任何可能的方法来制作它,它返回到打印命令,其中问题被问到了。

这是我目前拥有的:

import time
import sys
from random import randrange

text = "** Execute Long Intro **"

for c in text:
    sys.stdout.write(c)
    sys.stdout.flush()
    seconds = "0." + str(randrange(1, 2, 1))
    seconds = float(seconds)
    time.sleep(seconds)


time.sleep(1)


obj1 =input('\nDo you \na.) Rest on the ground \nb.) Find a way out of the jungle\n')
if obj1 in ('a'):
    print('You find a comfortable spot on the ground and drift into sleep...')
    time.sleep(.6)
    print('Zzz.\nZzz..\nZzz...')
    time.sleep(3)
    print('You wake to a strange noise, and work your way out of the jungle.') 
    time.sleep(1)

    print('You emerge out of the jungle and walk along the shoreline of a sunny beach')
    time.sleep(1)
    print('** Objective One Completed **')
    time.sleep(2)



elif obj1 == 'b':
    print('You manage to find a path out of the jungle and discover a beach,')
    time.sleep(1)
    print('** Objective One Completed **')
    time.sleep(2)

elif obj1 != ('a','b'):
    print('Uh.')

我们将不胜感激。

【问题讨论】:

    标签: python python-3.3


    【解决方案1】:

    使用while 循环:

    obj1 = input(...)
    
    while obj1 not in ('a', 'b'):
        obj1 = input("Invalid. Enter again: ")
    
    if obj1 == 'a':
        ...
    elif obj1 == 'b':
        ...
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用blessings 库。 安装它:

      pip install blessings
      

      而且用法很简单:

      from blessings import Terminal
      term = Terminal()
      print term.clear_bol
      print '\t{t.yellow}action{t.normal}|{t.green}Done!{t.normal}'.format(t=term)
      

      【讨论】:

      • 我用pip安装了bless,运行你提供的代码时出现无效语法错误。
      • 你明白了。简单修复错误。阅读祝福页面上的文档。我在代码中没有看到任何语法错误!
      • 这个答案没有解决问题。
      • 我真的不知道为什么我在 4 年前写了这个答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多