繁体   English   中英

我已经为要写入的字符串定义了一个 function,然后打印出每个单独的字母。 但它不适用于 input()

[英]I've defined a function for a string to be written, and then each individual letter to be printed out. But it won't work with an input()

我试图让它在同一行中逐个字母地打印出问题。

import sys
import time

def typing(msg):
    for z in msg:
        time.sleep(0.04)
        sys.stdout.write(z)
        sys.stdout.flush()

input(typing('What is your name?  '))

output: What is your name?None

您需要更改输入和键入的顺序:

import sys
import time

def typing(msg):
    for z in msg:
        time.sleep(0.04)
        sys.stdout.write(z)
        sys.stdout.flush()

typing(input())

试试这段代码。 我相信你不需要sys模块。

import time


def typing(msg):
    print(msg)
    
    for ch in msg:
        time.sleep(0.1)
        print(ch)
        time.sleep(0.1)
        



message = input('Whatever a message you want here: ')

typing(message)

动作 & Output:

Whatever a message you want here: Python is great
Python is great
P
y
t
h
o
n
 
i
s
 
g
r
e
a
t
>>> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM