通过在 python 中传递 print 语句来获取输入时,没有单词提示


def prompt(n):
    value= int(input(print("Please enter integer #", n, ": ",end='')))
    return value

在 Python 中给出的结果为 (如果 n=1(

>>>Please enter integer # 1 : None

为什么在执行上述功能时没有单词出现。

Python 中的 input 函数将一个字符串作为参数打印出来。 您正在做的是将print函数的返回值传递给input。 由于 print 不返回任何内容,因此input接收None作为参数,并将其打印在字符串之后。 相反,摆脱对print的调用,只使用:

def prompt(n):
    value = int(input("Please enter integer # %d : "%n))
    return value
不需要

print 语句。

print只是一个函数,默认情况下函数返回None

您应该使用:

int(input("Please enter integer #%d: " % n))

此外,如果input不可转换为int则会引发错误。

尝试使用:

def prompt(n):
    value= int(input(("Please enter integer #"+ str(n)+ ": ")))
    return value

最新更新