【问题标题】:"None" Prints on the end of the print command"None" 在打印命令结束时打印
【发布时间】:2015-09-17 02:03:34
【问题描述】:
def Tempurature(Lowest, Highest):
ok = False
while not ok:
    try:
       Input = int(input(print("\nEnter the Mid-Day tempurature for Day {}:".format(Counter))))
       if Input >= Lowest and Input <= Highest:
            ok = True
            return Input
       else:
            print ("Please enter a valid number between {} and {}".format(Lowest, Highest))
    except:
        print ("Please enter a number")

这是我的函数,我用它来将数字输入到这样的数组中

Counter = int(1)
TempDay = array.array ("i", range(31))
TempDay[Counter] = (Tempurature(-90, 60))

但是,当我调用该函数时,会打印以下内容

Enter the Mid-Day tempurature for Day 1:
None

关于如何摆脱“无”打印以使输入行位于“:”而不是“无”末尾的任何解决方案?

【问题讨论】:

  • 尝试在输入功能中删除打印
  • 另外,只是为了编程约定:变量、参数和函数(或方法,如果你称之为)应该以小写字母开头。类应以大写字母开头。如果其他人要阅读您的代码(如果它是一个复杂的程序),如果他们快速阅读它,他们可能会感到困惑。不过也没什么大不了的。

标签: python arrays function


【解决方案1】:

在您的 input 函数中丢弃 print 调用。 input 将自动打印 input 调用中的任何内容。

Input = int(input("\nEnter the Mid-Day tempurature for Day {}:".format(Counter))))

它打印None 的原因是由于print 调用;完成后它会返回None,并且由于input 会在阻止输入的同时愉快地打印您提供的任何内容,因此它会打印None,就好像您打算这样做一样。

【讨论】:

    【解决方案2】:

    您正在使用 None 参数调用 .format

    变量Counter 在您的function scope 中不可用。

    另外,您实际上是在print()return 上调用input(),您需要将字符串传递给input()

    【讨论】:

    • 可以在函数外定义。此外,如果将其定义为 None,那么您会看到“输入无天的中午温度”。他们在那里看到一个数字,所以这不是问题。
    • 正如我在回答中提到的,根据给定的代码,Counter 在函数范围内。关于将打印返回传递给输入的问题,我也给出了与您相同的响应。请不要仅仅因为您还有待处理的答案而对实际解决问题的答案投反对票。
    • 我没有反对它,因为我也做出了回答。我不赞成,因为您答案的前两部分与实际问题无关。
    • 我在他的代码中看到了两个错误,我回答了一些既解决了查询的错误,又解决了另一个他将很快遇到的错误。我错了吗?
    • 是的。我已经告诉过你了。 Counter 不是None,如果它,您将不会看到相同的输出。为什么不试试看呢?
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 2021-10-19
    • 1970-01-01
    • 2022-01-04
    • 2016-10-04
    • 2021-05-14
    相关资源
    最近更新 更多