简体   繁体   中英

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. I have a list called rand_list created by

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. it gives me {TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'} Am i comparing two different types of objects?

remainder2 = intermediary %2 produces an error because list.append() returns None, as the error states. You're also appending 2 random ints to the list every time. What you probably want is intermediary = rand_list[-1] which will return the last element in the list.

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. 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 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). You then try and perform an operation on this NoneType as if it were an Int. This wont work.

There are few mistakes in your code.

1) list.append() modyfies list in place, adding new element on the end. It does not return anything, or to be more precise, it returns None . So your intermediary == None and you cannot use % operator with 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:

is_odd = x % 2

or more readable:

is_odd = bool(x % 2)

2) The other problem is, that you missed if in your code. In fact, you didn't even use your remainder2 variable! 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. It does not produce error, as you shadow name of type int and builtin 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.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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