Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 素数函数_Python_Python 3.x - Fatal编程技术网

Python 素数函数

Python 素数函数,python,python-3.x,Python,Python 3.x,我对python的练习有问题。 我应该得到一个函数is_prime(n):定义素数。 程序应给出3种类型的输出; 1.n是乘法恒等式 2.n不是质数(n*d=n)。 3.n是素数 我有一个问题得到2。正常工作 我的输出是: 12不是质数。(2,“*”,6,“=”,12) 但应该是 12不是质数(2*6=12) 我的代码 def is_prime(n): msg1 = "is the multiplicative identity." msg2 = "is not a prime

我对python的练习有问题。 我应该得到一个函数is_prime(n):定义素数。 程序应给出3种类型的输出; 1.n是乘法恒等式 2.n不是质数(n*d=n)。 3.n是素数

我有一个问题得到2。正常工作 我的输出是: 12不是质数。(2,“*”,6,“=”,12)

但应该是

12不是质数(2*6=12)

我的代码

def is_prime(n):
    msg1 = "is the multiplicative identity."
    msg2 = "is not a prime number."
    msg3 = "is a prime."
    if n == 1:
        return str(n) + msg1

    for d in range(2, n):
        msg4 = (d, "*", n // d, "=", n)
        pass
        if (n % d) == 0:
         return str(n) + " " + msg2 + str(msg4)

else:
    return str(n) + " " + msg3

print(is_prime(12))
用于返回而不是连接

这是使用
f字符串
格式:

返回f'{n}{msg2}{msg4}'
括号
{}
中的任何内容都将像标准Python一样进行计算,或者将返回该值。它将自己转换为字符串,因此您无需对其执行
str()

所有代码:

def为素数(n):
msg1=“是乘法标识。”
msg2=“不是质数”
msg3=“是一个素数。”
如果n==1:
返回f'{n}{msg1}'
对于范围(2,n)内的d:
msg4=f'({d}*{n//d}={n}'
如果(n%d)==0:
返回f'{n}{msg2}{msg4}'
返回f'{n}{msg3}'
打印(是素数(12))

请使用下面的代码,因为您需要使用str()函数来打印预期结果

msg4 = str(d) + '*' + str(n // d) +'='+ str(n)
如果您想使用()的话,下面是完整的代码

def is_prime(n):
    msg1 = "is the multiplicative identity."
    msg2 = "is not a prime number."
    msg3 = "is a prime."
    if n == 1:
        return str(n) + msg1

    for d in range(2, n):
        msg4 = "(" + str(d) + '*' + str(n // d) +'='+ str(n)+ ")"
        pass
        if (n % d) == 0:
            return str(n) + " " + msg2 + str(msg4)

        else:
            return str(n) + " " + msg3

谢谢你的提示,但是输出仍然是;12不是质数(2,'*',6,'=',12)。它应该是12,而不是质数(2*6=12)。@Aru I修正了它。我错过了那里的
msg4
。这不适用于奇数复合数字。一旦它检查了
d=2
@nebiyuten,它将始终返回。他们的问题是如何以良好的字符串格式获得正确的返回。他们的代码是否实际返回适当的素数超出了他们所问的范围。如果您对此有更正,您可以提出建议,我很乐意为将来的读者添加它。@MyNameIsCaleb更正,但对于完整的解决方案,可能最好进行一次小的编辑。我做了一个小的编辑,如果你同意,你可以批准它,谢谢。你不必迭代到
n
。它足够做到
n//2
来判断它是素数,或者实际上,它足够做到
sqrt(n)
。为什么?因为(除非
n
是一个完美的平方),所有整数因子都将有一个小于平方根,一个大于平方根。因此,检查平方根以上的任何内容本质上就是寻找您已经寻找过的因素。