[英]How do I store information in lists
My goal is to create a program that checks whether a number is a prime number and displays the total number of factors if it is not a prime number.我的目标是创建一个程序来检查一个数字是否是素数,如果它不是素数则显示因子的总数。
I have started the code but I realize I need to store my factors in a list in order for the number of factors to be displayed.我已经开始编写代码,但我意识到我需要将我的因素存储在一个列表中,以便显示因素的数量。 Here is my (edited) Python code so far:
到目前为止,这是我的(已编辑)Python 代码:
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
for x in range(1, userNum+1):
if(userNum % x == 0):
factor = 0
numFactors.append(x)
factor = factor + 1
print(userNum,"is not a prime number")
print("Number of factors: ", factor)
break
else:
print(userNum,"is a prime number")
Can someone please tell me how to continue with storing factors into my list (numFactors)?有人可以告诉我如何继续将因子存储到我的列表中(numFactors)吗? Thank You!
谢谢你!
You need to use append()您需要使用 append()
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
for x in range(1, userNum+1):
if(userNum % x == 0):
factor = 0
numFactors.append(x)
factor = factor + 1
...
...
But there is a logical flaw in your code.但是您的代码中存在逻辑缺陷。 Try input as 99 .
尝试输入为99 。
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
# A prime number (or prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
# Hence range starting from 2
for x in range(2, userNum+1):
if(userNum % x == 0):
numFactors.append(x)
if len(numFactors) == 1:
print(userNum, " is a prime number")
else:
print(userNum,"is not a prime number")
print("Number of factors: ", len(numFactors))
print("Factors : ", numFactors)
Enter a number: 99
User Number: 99
99 is not a prime number
Number of factors: 5
Factors : [3, 9, 11, 33, 99]
First, you need to put the line that defines numFactors
, numFactors = []
, outside of and before the for
loop.首先,您需要将定义
numFactors
、 numFactors = []
的行放在for
循环之外和之前。 Otherwise, it will be continually set to an empty list for each iteration of the loop.否则,对于循环的每次迭代,它将不断设置为一个空列表。
Values can be added to the end of a list by using the list's append
method.可以使用列表的
append
方法将值添加到列表的末尾。 For example, numFactors.append(2)
would add 2
to the end of the list.例如,
numFactors.append(2)
会将2
添加到列表的末尾。
Your mistake is lacking check whether x is equal to userNum, that is in the end of the loop.您的错误是缺少检查 x 是否等于 userNum,即在循环结束时。
My other suggestion is you remove variable factor
as this can be known from the length of the numFactors list.我的另一个建议是您删除变量
factor
,因为这可以从 numFactors 列表的长度中得知。
userNum = int(input("Enter a number: "))
print("User Number: ", userNum)
numFactors = []
for x in range(1, userNum + 1):
if userNum % x == 0:
numFactors.append(x)
if userNum == x:
print("Factors: " + str(numFactors))
if len(numFactors) == 2:
print(userNum, "is a prime number")
else:
print(userNum, "is not a prime number")
print("Number of factors: ", len(numFactors))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.