系统标准输出写入带有变量的()和.flush()

2024-10-01 07:44:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在做一个RPG游戏,我喜欢系统标准输出写入()和.flush()使其具有键入字母的效果。但是当我把一个变量输入到我想输入的句子中时,它不会输出句子的任何一部分,甚至连我在引号中的部分(非变量)也不会。我想知道是否有一种方法可以避免我为变量和引号中的内容编写单独的代码块,或者让它键入变量。在

if girl1 == True:
   t="You and",girl1realname,"have found a sandy beach while looking for a 
   place of shelter. \nWhether you look left or right, the beach ranges for 
   miles.\n"
   y=girl1realname+": 'Hey, I guess I always wanted a house on the beach...I 
   think. I can't really remember.'\n"
   for char in (t):
       time.sleep(0.05)
       sys.stdout.write(char)
       sys.stdout.flush()
   time.sleep(1)
   for char in (y):
       time.sleep(0.05)
       sys.stdout.write(char)
       sys.stdout.flush()

Tags: theinfor键入timestdoutsyssleep
1条回答
网友
1楼 · 发布于 2024-10-01 07:44:17

t的定义中,逗号将使其成为一个元组。迭代,然后一次写入每个块("You and"girl1realname,和"have found..."),而不是每个块中的每个字符。将逗号替换为+。在

你对y的定义似乎很好。在

至于重用,您可以定义一个函数,如:

def typeout(x):
  for char in x:
    time.sleep(0.05)
    sys.stdout.write(char)
    sys.stdout.flush()

使用方式:

^{pr2}$

相关问题 更多 >