You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 1使用sys模块逐字符打印输入提示符时出现None的问题

问题原因

你遇到的None是因为input()函数会将传入的参数作为提示文本输出。你的liner函数没有定义返回值,Python中无返回值的函数默认返回None,所以执行input(liner("Enter the input here."))时,liner完成逐字符打印后,input会把liner返回的None追加显示出来,最终出现Enter the input here.None。

修复方案

有两种简单的修复方式:

方式一:让liner函数返回空字符串

修改liner函数,在末尾添加返回空字符串的语句,这样input接收的参数为空,就不会额外输出None:

import sys
import time

def liner(prompt):
  for i in prompt:
    sys.stdout.write(i)
    sys.stdout.flush()
    time.sleep(0.05)
  return ""  # 添加返回空字符串

x = input(liner("Enter the input here."))

方式二:分开调用liner和input

先调用liner打印提示内容,再单独调用input(无需传入提示参数):

import sys
import time

def liner(prompt):
  for i in prompt:
    sys.stdout.write(i)
    sys.stdout.flush()
    time.sleep(0.05)

liner("Enter the input here.")
x = input()

两种方式都能得到你预期的输出:Enter the input here.

内容的提问来源于stack exchange,提问作者Blackbeard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:35:22