【问题标题】:Unwanted None printed when asking for input [duplicate]要求输入时不打印不想要的内容[重复]
【发布时间】:2020-10-27 11:05:51
【问题描述】:

我的代码似乎在代码末尾打印了None。我读到这是由于几个打印语句,但我不确定如何在我的情况下解决它:

def type(str):
    for char in str:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.075)
    time.sleep(1.5)

input1 = input(type("""\na. You go to the printer
b. Go to keyboard"""))

输出:

a. You go to the printer
b. Go to keyboardNone

最后如何去掉多余的None

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    type 这里返回None,它被馈送到input,然后在type 执行完毕后写入,所以,首先,我建议将函数名称type 更改为别的东西,因为type 是一个内置的 Python 函数,并且通过定义你自己的函数,你正在隐藏原始函数,其次,你可以在请求输入之前调用 type

    type("""\na. You go to the printer
    b. Go to keyboard""")
    
    input1 = input()
    

    关于type的注释也可以说是type的参数名strstr也是Python内置函数,用它作为参数名会影响内置函数,但仅限于定义它的函数范围内 (type),所以我建议也更改它。

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 2016-01-14
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      相关资源
      最近更新 更多