【问题标题】:Having problems with function definition in pythonpython中的函数定义有问题
【发布时间】:2019-07-06 19:33:58
【问题描述】:
def prompt(n):
    value=int(input("Please enter integer #", n ,":" , sep=""))
    return value

value1=prompt(1)

错误:

value=int(input("请输入整数#", n ,":" , sep="")) 类型错误: input() 不接受关键字参数

【问题讨论】:

  • 您提供的参数看起来像是用于print()。它们对input() 毫无意义。
  • 如何得到如下输出:' Please enter integer #1:' for prompt(1) ??

标签: python function-definition


【解决方案1】:

python 中的 input() 内置函数只需要 1 个参数 - prompt。请参考input function的python文档

编辑:根据您的评论,您需要更新提示以包含您发送的参数。请参阅下面的代码。正如 chris 在 cmets 中提到的,f 字符串仅适用于 Python 3.6 版

def prompt(n): 
    value=int(input(f"Please enter integer {}".format(n))) 
    return value

对于

def prompt(n): 
    value=int(input("Please enter integer {}".format(n))) 
    return value

【讨论】:

  • 是的,它现在可以工作了。你能参考python中函数定义的更新文档吗?
  • 值得一提的是 f-formatting 仅在 python 3.6 或更高版本中支持。
  • @Abhi 还有不错的%-formatting 以及str.format()
  • 看来我学习python的书已经过时了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2021-08-18
  • 2019-03-12
  • 2015-06-02
相关资源
最近更新 更多