Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法在字典的键值对中包含用户输入提示或time.sleep()函数?_Python_Python 3.x_Dictionary_Input_Time - Fatal编程技术网

Python 有没有办法在字典的键值对中包含用户输入提示或time.sleep()函数?

Python 有没有办法在字典的键值对中包含用户输入提示或time.sleep()函数?,python,python-3.x,dictionary,input,time,Python,Python 3.x,Dictionary,Input,Time,我正在开发一个Python文本RPG,我正在使用字典向玩家提供他们访问的区域的初始信息。(参见代码示例)。当播放器键入“look”或“examine”时,我希望控制台打印出检查键值中的内容。我想让它做的是一次打印一段文本,在继续之前等待播放器按enter键,或者至少等待几秒钟再打印下一个块。有没有办法做到这一点?也许我是从错误的方向来的 import time import sys def prompt(): print("\n" + "========================

我正在开发一个Python文本RPG,我正在使用字典向玩家提供他们访问的区域的初始信息。(参见代码示例)。当播放器键入“look”或“examine”时,我希望控制台打印出检查键值中的内容。我想让它做的是一次打印一段文本,在继续之前等待播放器按enter键,或者至少等待几秒钟再打印下一个块。有没有办法做到这一点?也许我是从错误的方向来的

import time
import sys

def prompt():
    print("\n" + "=========================")
    print("What would you like to do?")
    player_action = input("> ")
    acceptable_actions = ['move', 'go', 'travel', 'walk', 'quit', 'examine', 'inspect', 'interact', 'look']
    while player_action.lower() not in acceptable_actions:
        print("Unknown action, try again.\n")
        player_action = input("> ")
    if player_action.lower() == 'quit':
        sys.exit()
    elif player_action.lower() in ['move', 'go', 'travel', 'walk']:
        player_move(player_action.lower())
    elif player_action.lower() in ['examine', 'inspect', 'interact', 'look']:
        player_examine(player_action.lower())

def player_examine(player_action):
    if zonemap[myPlayer.location][SOLVED]:
        print("There's nothing more here to examine.")
    elif zonemap[myPlayer.location][EXAMINATION]:
        slowprint(zonemap[myPlayer.location][EXAMINATION])

ZONENAME = ''
DESCRIPTION = 'description'
EXAMINATION = 'examine'
SOLVED = False
UP = 'up', 'north'
DOWN = 'down', 'south'
LEFT = 'left', 'west'
RIGHT = 'right', 'east'

zonemap = {
    'Fields': {
        ZONENAME: "Western Fields",
        DESCRIPTION: "A grassy field to the west of town.",
        EXAMINATION: "The grass in this field is extremely soft." + input("> ") + "The wind feels cool on your face." + time.sleep(2) + "The sun is beginning to set.",
        SOLVED: False,
        UP: "The Mountains",
        DOWN: "The Town",
        LEFT: "", 
        RIGHT: "The Ocean",
    },
尝试使用time.sleep()方法时,出现以下错误:

TypeError: can only concatenate str (not "NoneType") to str

尝试使用输入(“>”)函数时,文本只需在不等待的情况下打印。

您的方法不起作用,因为您在构建字典时会立即调用
input()
time.sleep()
函数<例如,code>time.sleep()返回
None
,这就是为什么会出现错误

稍后,当您从字典中检索到值并实际希望“慢打印”描述时,需要调用这些函数

您可以通过多种不同的方式来实现。你可以

  • 使用字符串序列(如列表或元组)而不是单个字符串,并让
    slowprint()
    函数接受序列并在打印每个元素后暂停

  • 使用一系列字符串并混合使用
    slowprint()
    查找的特殊值来执行不同的操作,如睡眠或请求输入

  • 在字典中存储一个函数,然后调用该函数。函数也是对象,就像字符串一样。该功能将处理所有打印和暂停

例如,存储字符串的元组:

EXAMINATION: (
    "The grass in this field is extremely soft.",
    "The wind feels cool on your face.",
    "The sun is beginning to set.",
)
然后让您的
slowprint()
函数处理:

def slowprint(lines):
    """Print each line with a pause in between"""
    for line in lines:
        print(line)
        input("> ")   # or use time.sleep(2), or some other technique
第二个选项是插入特殊值,它使您能够将各种额外功能委托给其他代码。您需要测试序列中对象的类型,但这将允许您在检查描述中插入任意操作。就像睡觉和要求用户按键之间的区别一样:

class ExaminationAction:
    def do_action(self):
        # the default is to do nothing
        return

class Sleep(ExaminationAction):
    def __init__(self, duration):
        self.duration = duration

    def do_action(self):
        time.sleep(self.duration)

class Prompt(ExaminationAction):
    def __init__(self, prompt):
        self.prompt = prompt

    def do_action(self):
        return input(self.prompt)
并使用
slowprint()
函数查找以下实例:

def slowprint(examine_lines):
    for action_or_line in examine_lines:
        if isinstance(action_or_line, ExamineAction):
            # special action, execute it
            action_or_line.do_action()
        else:
            # string, print it
            print(action_or_line)
你可以采取任何数量的行动;关键是它们都是子类
检查动作
,因此可以区别于普通字符串。将它们放入
检查的顺序中
键:

EXAMINATION: (
    "The grass in this field is extremely soft.",
    Prompt("> "),
    "The wind feels cool on your face.",
    Sleep(2),
    "The sun is beginning to set.",
)

可能性是无穷的。

谢谢你,Martijn,我会试试看哪一种最适合我。