为什么它在输出中显示None None

问题描述:

我得到一个无以下代码的输出,我不知道它为什么是? 有人可以解释我为什么是这样以及它如何修复? 这里的代码,为什么它在输出中显示None None

def randomQuestions(): 
     q1 = 1 
     q2 = 2 
     for i in range(1): 
      if random.randint(1,2)==1: 
       a1=int(input(print("Please Enter a Number that is not prime: ",end=""))) 
       if a1>1: 
        for i in range (2,a1): 
         if (a1%i)==0: 
          print("Correct",a1,"is Not a prime number") 
          break 
        else: 
         print("Incorrect",a1,"is a Prime number") 
       else: 
        print("Incorrect",a1,"is a Prime number") 
      else: 
       a2=int(input(print("Please Enter a Number that is positive: ",end=""))) 
       if a2>0: 
        print("Correct",a2,"is Positive") 
       elif a2==0: 
        print(a2,'Is neither Positive Nor Negative') 
       else: 
        print('Incorrect',a2,'is Not Positive') 
    randomQuestions() 

这是输出;

Please Enter a Number that is not prime: None 

input功能已经打印输入消息,所以你不需要打印功能。它显示None,因为print函数返回None。更改

int(input(print("Please Enter a Number that is positive: "))) 

int(input("Please Enter a Number that is positive: ")) 

做就是另一个输入,以及。

如果你想成为一个新的行中输入的输入,也可以做

int(input("Please Enter a Number that is positive: \n")) 
+0

@ayban欣赏你的答案:)但现在它给出了以下错误; TypeError:input()不带关键字参数 –

+0

确实如此,您还需要删除'end =“”'。 – ayhan

+0

@ayban它的工作原理:)现在我明白我犯的错误是什么。在此先感谢:) –

请删除打印语句。

def randomQuestions(): 
      q1 = 1 
      q2 = 2 
      for i in range(1): 
       if random.randint(1,2)==1: 
        a1=int(input("Please Enter a Number that is not prime: ")) 

        if a1>1: 
         for i in range (2,a1): 
          if (a1%i)==0: 
           print("Correct",a1,"is Not a prime number") 
           break 
         else: 
          print("Incorrect",a1,"is a Prime number") 
        else: 
         print("Incorrect",a1,"is a Prime number") 
       else: 
        a2=int(input("Please Enter a Number that is positive: ")) 

        if a2>0: 
         print("Correct",a2,"is Positive") 
        elif a2==0: 
         print(a2,'Is neither Positive Nor Negative') 
        else: 
         print('Incorrect',a2,'is Not Positive') 
+0

修复它:)在此先感谢:) –

无需为您的输入使用打印包装。

https://docs.python.org/3/library/functions.html#input

此外,它可能不是一个坏主意,这样做,然后再继续NoneType检查,但下面的代码应该让你的工作。

import random 


def randomQuestions(): 
    q1 = 1 
    q2 = 2 

    for i in range(1): 
     if random.randint(1,2) == 1: 
      a1 = int(input("Please Enter a Number that is not prime: ")) 
      if a1 > 1: 
       for i in range (2, a1): 
        if (a1 % i) == 0: 
         print("Correct", a1, "is Not a prime number") 
         break 
       else: 
        print("Incorrect", a1, "is a Prime number") 
      else: 
       print("Incorrect", a1, "is a Prime number") 
     else: 
      a2 = int(input("Please Enter a Number that is positive: ")) 
      if a2 > 0: 
       print("Correct", a2, "is Positive") 
      elif a2 == 0: 
       print(a2, 'Is neither Positive Nor Negative') 
      else: 
       print('Incorrect', a2, 'is Not Positive') 

if __name__ == '__main__': 
    randomQuestions()