好的,所以我试图为课程编写一个模块来加载一个基本的素数检查器。我们有样本可供查看,而且我要复制到目前为止的内容......但是我对如何编写此内容感到很遗憾,然后让程序实际调用该模块(应该是导入xxxx.py吧?)
不要太苛刻,我不是我班上唯一一个与之斗争的人。 Youtube和我在这里的搜索结果确实没有帮助。提前感谢另一个基本问题。#!/usr/bin/env python3
#Program Name
print("Prime Number Checker")
def factor_count(num):
factor_count = 0
print("The factors are: ")
def main():
choice = "y"
while choice.lower() == "y":
#user input
num = int(input("Please enter a number: "))
if num <= 1:
print("please choose a value greater than 1. ")
print()
else:
for i in range(1, num+1):
if num%i == 0:
print(i)
if factor_count == 2:
print(num, "is prime")
else:
print(num, "is not prime!")
if __name__ == "__main__" :
main()
print()
print("goodbye")
答案 0 :(得分:0)
要点,这就是我编写程序的方式:
factor_count
,请不要使用相同的名称。因此,当您写if factor_count == 2
时总是错误的。choice
的值def main():
choice = "y"
while choice.lower() == "y":
factor_count = 0
# user input
num = int(input("Please enter a number: "))
if num <= 1:
print("please choose a value greater than 1. ")
print()
else:
for i in range(2, num):
if num % i == 0:
print(i)
factor_count += 1
if factor_count == 0:
print(num, "is prime")
else:
print(num, "is not prime!")
choice = input('Do you want to continue?(y/n) : ')
if __name__ == "__main__":
print("Prime Number Checker")
main()
print()
print("goodbye")