简体   繁体   English

如何在列表中存储信息

[英]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()

Your syntax changed as below.您的语法更改如下。

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

Corrected solution更正的解决方案

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)

Output Output

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.首先,您需要将定义numFactorsnumFactors = []的行放在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.

相关问题 如何根据大量输入信息制作空列表? - how do I make empty lists based on a number of input information? 运行程序时如何存储信息 - How do I store Information when I run the program 我在哪里存储用户个人信息 - Where do I store user personal information 如何按列分组并将信息存储在列表中以避免丢失? - How to group by columns and store information in lists to avoid losing it? 如何在保存所有键值信息的同时从python中的两个列表创建字典? - How do I create a dictionary from two lists in python while maintaining all key value information? 如何在 Python 中修改列表中的列表? - How do i modify lists in lists in Python? 如何在python中存储用户信息,以便下次可以继续使用? - How do I store the user information in python so that i can keep using it next time? 如何永久存储可以编辑的列表信息? - How do I store information of a list permanently which I can then edit? 如何在Python中将多个列表存储到文本文件中,然后随机选择一个? - How do I store several lists into a textfile in python and then choice one at random? 如何以编程方式在iPhone应用程序商店中提取(向iTunes的网址的)列表/数组? - How do I programmatically pull lists/arrays of (itunes urls to) apps in the iphone app store?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM