【问题标题】:Input returns None when using function to print a string with a delay使用函数延迟打印字符串时,输入返回无
【发布时间】:2020-12-30 00:22:50
【问题描述】:

这是我延迟打印的后续问题。但是这次我在尝试输入 print_with_delay(text) 函数时被卡住了。在打印发生之后并且在我可以在控制台中输入任何内容之前它返回 None 。有什么办法可以解决这个问题?

代码:

import time

# Delay text function to create realism for console dialogues
# Counts the length of the string and applies this to the delay
# Greatly improves visibility in the code
# No more time.sleep() between all print dialogues
def print_with_delay(text):
    delay = len(text) / 100 + 1.2
    time.sleep(delay)
    print(text)
    # if not text:
    #    return text

def start_game():
    username = input(print_with_delay("Hello adventurer, I am Tony Tales, and what is your name?"))
    print_with_delay(f"Nice to meet you {username}.")
    print_with_delay("They call me Tony Tales, because I am a very talented tale teller.")
    create_story = input (print_with_delay("I can sense greatness from you. Would you like to create a story with me? [Y/N]"))
    
    if create_story == "y" or create_story == "Y":
        print_with_delay("Great!")
    else:
        print_with_delay("Oooh? That is disappointing. Here, take this!")
        print_with_delay("Tony Tales stabs you with a Pointy Pen...")
        print_with_delay("You feel dizzy and faint...")
        print_with_delay("The story ended.")
        exit()

输出:

Hello adventurer, I am Tony Tales, and what is your name?
NoneTim
Nice to meet you Tim.
They call me Tony Tales, because I am a very talented tale teller.
I can sense greatness from you. Would you like to create a story with me? [Y/N]
NoneY
Great!

【问题讨论】:

    标签: python string input return console-application


    【解决方案1】:

    您可以编写一个通用的with_delay 函数,该函数将在延迟后使用给定的参数运行一个任意 函数。您可以将其默认为print 以模拟print_with_delay

    def with_delay(text, f=print):
        delay = len(text) / 100 + 1.2
        time.sleep(delay)
        return f(text)
    
    
    def start_game():
        username = with_delay("Hello adventurer, I am Tony Tales, and what is your name?", input)
        with_delay(f"Nice to meet you {username}.")
        with_delay("They call me Tony Tales, because I am a very talented tale teller.")
        create_story = with_delay("I can sense greatness from you. Would you like to create a story with me? [Y/N]")
    

    【讨论】:

    • 谢谢。我不知道这是可能的。这是一个如此愚蠢的问题,以至于它被否决了吗?我试图找到类似的帖子,但我无法理解它。
    【解决方案2】:

    start_game 的前两行替换为以下内容:

    username = input("Hello adventurer, I am Tony Tales, and what is your name? ")
    print_with_delay(f"Nice to meet you %s ." % username)
    

    您需要从输入中删除print_with_delay,因为它是一个返回None的函数

    【讨论】:

    • 是的,我明白这一点,但我想对将要显示的每个文本应用延迟。输入之前的字符串也是如此。
    • @Tim 然后将time.sleep(2) 放在username 行之前
    • 我试图避免 time.sleep() 以防止代码混乱并自动延迟。所以你是说我想要达到的目标是不可能的?
    • 如果您使用print_with_delay输出提示,则无需将提示字符串传递给input
    • @chepner 谢谢,我以前试过。它的工作方式应该如此。但我曾希望有另一种方法。
    【解决方案3】:

    正如@Chepner 提到的, input() 方法不需要字符串参数。

    它是这样工作的:

    def start_game():
        print_with_delay("Hello adventurer, I am Tony Tales, and what is your name?")
        username = input()
        print_with_delay(f"Nice to meet you {username}.")
        print_with_delay("They call me Tony Tales, because I am a very talented tale teller.")
        print_with_delay("I can sense greatness from you. Would you like to create a story with me? [Y/N]")
        create_story = input()
    

    我曾希望有另一种方法来解决它。但这暂时可以完成这项工作。

    编辑:

    上面显示的是一种解决方法。检查@Chepner 他的答案以获得更好的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多