【发布时间】:2017-01-13 14:58:36
【问题描述】:
我正在测试一个打印功能,它看起来好像提示正在输入,定义为 type()
我想使用 type 函数存储原始输入:
from time import sleep
import sys
from random import uniform
def type(s):
for c in s:
sys.stdout.write('%s' % c)
sys.stdout.flush()
sleep(uniform(0, 0.3))
name = raw_input(type("What is your name? "))
type("Hello " + name +"\n")
这是代码的输出:
你叫什么名字?无
仍然允许用户输入,在“无”之后,输出将正确打印,没有“无”。有没有办法绕过这个?
在这个提示中,我想使用 type 函数打印所有内容。
【问题讨论】:
-
问题是您将
type()传递给raw_input。type隐式返回None,因为您没有给它明确的返回值。也许尝试将raw_input放入type并返回结果。顺便说一句,名称type已经属于内置函数type,隐藏它是不好的做法。为您的函数选择一个不同的名称,而不是type。 -
Python 已经有一个函数
type()。将您的重命名为type_()或其他名称
标签: python python-2.7