Python作为Prime和function的代码

2024-06-01 06:03:44 发布

您现在位置:Python中文网/ 问答频道 /正文

陷入这个问题,我找不到答案,我的代码不断失败。在

编写一个名为specialPrime的函数,该函数接受一个整数作为参数,如果整数是一个质数且整数平方的长度小于6位数,则返回True;如果不是质数或整数平方大于6位数,则返回False。编写一个程序,提示用户键入一个整数,并使用specialPrime函数来确定该整数是否特殊。在

示例交互 输入数字:140 140不是一个特殊的素数。 输入数字:89 89是一个特殊的素数。在

我的代码

def specialPrime(isPrime,G6):
isPrime= int(input('Enter a number:')
    if isPrime < 2 return False 
    elif isPrime == 2
        return True  
    for n in range(2, x)
        if x % n ==0:
        return False
    return True
G6 = len(isPrime**2)
    if G6 > 6: return False
    else
    return True
while True
print( isPrime + 'is a special number')
else
print( isPrime + 'is not a special prime')

`


Tags: 函数代码falsetruenumberreturnif数字
2条回答

您可以修改代码,为special_prime(x)的两个需求分别使用一对助手函数:

def squared_less_than_six_digits(x):
  return len(str(x**2)) < 6

def is_prime(x):
  if x < 2:
    return False
  else:
    for n in range(2, x):
      if x % n == 0:
        return False
    return True

def special_prime(x):
  return is_prime(x) and squared_less_than_six_digits(x)

def main():
  user_input = 0
  while True:
    try:
      user_input = int(input("Please enter an integer:"))       
    except ValueError:
      print("Error: You did not enter a integer. Please try again.")
      continue
    else:
      print("You entered the integer {}. Its square is {}.".format(user_input, user_input**2))
      break
  if special_prime(user_input):
    print("It is a special prime.")
  else:
    print("It is not a special prime.")

if __name__ == "__main__":
  main()

尝试上面的代码here!

测试:

  • 平方小于六位数的素数:

    Please enter an integer: 2
    You entered the integer 2. Its square is 4.
    It is a special prime.
    
  • 质数大于或等于六位数的素数:

    Please enter an integer: 317
    You entered the integer 317. Its square is 100489.
    It is not a special prime.
    
  • 平方小于六位数的非素数:

    Please enter an integer: 1
    You entered the integer 1. Its square is 1.
    It is not a special prime.
    
  • 平方大于或等于六位数的非素数:

    Please enter an integer: 318
    You entered the integer 318. Its square is 101124.
    It is not a special prime.
    

第一个:

Write a function called specialPrime which takes in an integer as an argument and returns True [or] False

你的函数不接受一个整数作为参数,它接受两个参数…我不确定它们的目的是什么,因为不管怎样你都忽略了它们。所以,从这个开始。另外,给它一个有意义的名字。isPrime听起来像一个标志,它告诉你一个数是质数,还是一个函数,用来判断一个数是否素数,而不是一个可能是素数也可能不是质数的候选数。所以:

def specialPrime(number):

代码的下一部分很接近,但也有问题:

  • 您应该测试作为参数得到的值,而不是从input得到的完全不同的值。在
  • ifeliffor,以及Python中所有其他“复合语句”都需要冒号。在
  • if等,具有多行正文的语句需要缩进这些正文。在
  • 什么是x?您在测试isPrime,突然又在测试另一个甚至在任何地方都没有定义的变量。在
  • 如果这个数是== 2,并且如果它是> 2,但是2和这个数之间没有除数,那么你return True。这意味着你没有测试其他条件;你只是假设它总是正确的。在

所以:

^{pr2}$

所有这些都可以通过多种方式进行改进,但这些都是使其作为Python代码有意义的最小更改。在


接下来,你要计算一个数的长度。数字没有长度。可以取数字的字符串表示长度:

^{3}$

…或者您可以做算术来测试位数:

square = number**2
if square > 999999:

另外,注意名称digitssquare,它们告诉你这是一个数字的计数,或者一个数字的平方,而不是{},这告诉你它是由6个主要欧盟国家组成的一个组。在

无论哪种方式,第一个块中的冒号和缩进都会出现一些相同的问题,您需要用同样的方法来解决这些问题。在


最后:

Write a program which prompts the user to type in an integer and uses you specialPrime function to determine whether or not the integer is special.

这里没有关于while True类型循环的内容,它是对程序的合理扩展,但是首先要让基础工作起来。在

所以你需要提示用户输入一个整数。这里是您使用^{的地方:

number = input('Enter a number:')

但是input的结果是一个字符串。如果用户输入23,得到的是字符串'23'。因此,您需要调用int来转换它:

number = int(input('Enter a number:'))

现在您必须调用函数:

if specialPrime(number):

同样,冒号和缩进也有一些需要修正的错误。在


在所有这些修复之后,代码将运行。如果你的测试没有逻辑错误,它会给出正确的答案。如果有…你可以从那里调试。在

相关问题 更多 >