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