简体   繁体   English

“无”在打印命令的末尾打印

[英]"None" Prints on the end of the print command

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")

So this is my Function, I use it to input numbers into an array like so所以这是我的函数,我用它来将数字输入到数组中

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

However when I call up the function, the following prints但是,当我调用该函数时,会打印以下内容

Enter the Mid-Day tempurature for Day 1:
None

Any solutions on how to get rid of the "None" print to make the input line at the ":" rather than at the end of the "None"?关于如何摆脱“无”打印以使输入行位于“:”而不是“无”末尾的任何解决方案?

Ditch the print call inside of your input function.放弃input函数内部的print调用。 input will automatically print anything inside of the input call. input将自动打印input调用中的任何内容。

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

The reason it's printing None is due to the print call;它打印None的原因是由于print调用; after its completion it returns None , and since input will happily print whatever you give it while blocking for input, it's printing the None as if you had intended to do that.完成后,它返回None ,并且由于input会很高兴地打印您在阻止输入时提供的任何内容,因此它正在打印None ,就好像您打算这样做一样。

You are calling .format with a None argument.您正在使用None参数调用.format

The variable Counter is not available inside your function scope .变量Counter在您的函数范围内不可用。

Also, you are actually calling input() on the return of print() , you need to pass in the string to input() .此外,您实际上是在print()返回时调用input() ,您需要将字符串传递给input()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM