简体   繁体   English

在Python中找到整数和列表元素的余数

[英]finding a remainder of an integer and a list element in python

I am trying to create a program that will create 10 random numbers and test if they are even or odd. 我正在尝试创建一个程序,该程序将创建10个随机数并测试它们是偶数还是奇数。 I have a list called rand_list created by 我有一个名为rand_list的列表,由创建

rand_list = []

for int in range(10):  
    rand_list.append(random.randint(1,1000))
    intermediary = rand_list.append(random.randint(1,1000))
    remainder2 = intermediary % 2 #this statement produces an error
    print("the \i number is even \n", rand_list[int])
    else:
        print("the \i number is odd \n", rand_list[int])

I do not understand why if remainder == 0: does not work. 我不明白为什么余数== 0:不起作用。 it gives me {TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'} Am i comparing two different types of objects? 它给我{TypeError:%不支持的操作数类型:'NoneType'和'int'}我是否在比较两种不同类型的对象?

remainder2 = intermediary %2 produces an error because list.append() returns None, as the error states. list.append() remainder2 = intermediary %2会产生错误,因为list.append()返回None(作为错误状态)。 You're also appending 2 random ints to the list every time. 您还会每次在列表中添加2个随机整数。 What you probably want is intermediary = rand_list[-1] which will return the last element in the list. 您可能想要的是intermediary = rand_list[-1] ,它将返回列表中的最后一个元素。

In addition, please provide working/well-formatted code in your questions. 另外,请在您的问题中提供有效/格式正确的代码。 The indentation is a little confusing and you're missing a line of code as well. 缩进有点令人困惑,并且您也缺少一行代码。

the append() function does not return anything. append()函数不返回任何内容。 It modifies the list in place. 它会修改列表。 What you would want to do is something like this 您想要做的就是这样

rand_list.append(random.randint(1,1000))
intermediary = rand_list[-1]
remander2= intermediary % 2

or perhaps cleaner: 也许更清洁:

intermediary = random.randint(1,1000)
rand_list.append(intermediary)
remainder2 = intermdiary % 2

What about as your if statement, you use 如果使用if语句呢?

if number%2 == 0
  # is even
else
  # is odd

You are receiving the TyperError because you are assigning a method call to a veritable - this does nothing at all (hence the NoneType in your error message). 您收到TyperError的原因是,您正在向Veritable分配方法调用-根本不执行任何操作(因此,错误消息中为NoneType)。 You then try and perform an operation on this NoneType as if it were an Int. 然后,您尝试对此NoneType进行操作,就好像它是一个Int。 This wont work. 这不会工作。

There are few mistakes in your code. 您的代码中几乎没有错误。

1) list.append() modyfies list in place, adding new element on the end. 1) list.append() modyfies列表就位,最后添加新元素。 It does not return anything, or to be more precise, it returns None . 它不返回任何东西,或更确切地说,它返回None So your intermediary == None and you cannot use % operator with None ... 因此,您的intermediary == None并且无法将%运算符与None ...

You can assign your random number to variable: 您可以将随机数分配给变量:

x = random.randint(1,1000)
rand_list.append(x) # append it to your list
remainder = x % 2 # take remainder using

Because you only care if it is zero or not, you can later use fact, that zero is equal to False in boolean context, and all other number are treated as True So you can write instead: 因为您只关心它是否为零,所以以后可以使用事实,即在布尔上下文中零等于False ,而所有其他数字都被视为True因此您可以这样写:

is_odd = x % 2

or more readable: 或更可读:

is_odd = bool(x % 2)

2) The other problem is, that you missed if in your code. 2)另一个问题是, if您在代码中错过了。 In fact, you didn't even use your remainder2 variable! 实际上,您甚至没有使用过remainder2变量! The correct code would be: 正确的代码是:

if (remainder2 == 0):
    print("the \i number is even \n", rand_list[int])
else:
    print("the \i number is odd \n", rand_list[int])

or using my advice above: 或使用我上面的建议:

if is_odd:
    print("the \i number is odd \n", x)
else:
    print("the \i number is even \n", x)    

3) The last thing is, that as @Padraic Cunningham pointed out, you should not use int as variable name. 3)最后一件事是,正如@Padraic Cunningham指出的那样,您不应使用int作为变量名。 It does not produce error, as you shadow name of type int and builtin int() . 它不会产生错误,因为您会遮盖类型int和内置int()

To sum up, your code could look like this: 综上所述,您的代码如下所示:

rand_list = []

for i in range(10):  
    x = random.randint(1,1000)
    if (x % 2):
        print(i, '-', x, "- the number is odd.")
    else:
        print(i, '-', x, "- the number is even.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM