对于以下代码的输出,我得到一个无,我不知道为什么会这样? 有人可以解释一下它为什么以及如何修复它? 这是代码,
Please Enter a Number that is not prime: None
这是输出;
while(temp!=NULL)
{
N=temp->next->data;
...
答案 0 :(得分:2)
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"))
答案 1 :(得分:0)
请删除打印声明。
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')
答案 2 :(得分: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()