Function that returns a tuple gives TypeError: NoneType object is not iterable

2024/9/16 23:34:15

What does this error mean? I'm trying to make a function that returns a tuple. I'm sure i'm doing all wrong. Any help is appreciated.

from random import randint
A = randint(1,3)
B = randint(1,3)
def make_them_different(a,b):while a == b:a = randint(1,3)b = randint(1,3)return (a,b)
new_A, new_B = make_them_different(A,B)
Answer

Your code returns None if a != b.

Since, you have the return statement inside the while loop, if the while loop never gets executed, Python returns the default value of None which cannot be assigned to new_A, new_B.

>>> print make_them_different(2, 3)
None>>> print make_them_different(2, 2)
(2, 1)

You could fix this by returning the default values (since they are different and that's what you intend to do)

def make_them_different(a,b):while a == b:a = randint(1,3)b = randint(1,3)return (a,b)  # Dedented the return line.

Demo -

>>> make_them_different(2, 2)
(3, 2)
>>> make_them_different(2, 3)
(2, 3)
https://en.xdnf.cn/q/73008.html

Related Q&A

Error when plotting DataFrame containing NaN with Pandas 0.12.0 and Matplotlib 1.3.1 on Python 3.3.2

First of all, this question is not the same as this one.The problem Im having is that when I try to plot a DataFrame which contains a numpy NaN in one cell, I get an error:C:\>\Python33x86\python.ex…

Java method which can provide the same output as Python method for HMAC-SHA256 in Hex

I am now trying to encode the string using HMAC-SHA256 using Java. The encoded string required to match another set of encoded string generated by Python using hmac.new(mySecret, myPolicy, hashlib.sha2…

How to get response from scrapy.Request without callback?

I want to send a request and wait for a response from the server in order to perform action-dependent actions. I write the followingresp = yield scrapy.Request(*kwargs)and got None in resp. In document…

install error thinks pythonpath is empty

I am trying to install the scikits.nufft package here I download the zip file, unpack and cd to the directory. It contains a setup.py file so I run python setup.py installbut it gives me the following …

Conditionally installing importlib on python2.6

I have a python library that has a dependency on importlib. importlib is in the standard library in Python 2.7, but is a third-party package for older pythons. I typically keep my dependencies in a pip…

Python/pandas: Find matching values from two dataframes and return third value

I have two different dataframes (df1, df2) with completely different shapes: df1: (64, 6); df2: (564, 9). df1 contains a column (df1.objectdesc) which has values (strings) that can also be found in a c…

random.choice broken with dicts

The random.choice input is supposed to be a sequence. This causes odd behavior with a dict, which is not a sequence type but can be subscripted like one: >>> d = {0: spam, 1: eggs, 3: potato} …

Tornado [Errno 24] Too many open files [duplicate]

This question already has an answer here:Tornado "error: [Errno 24] Too many open files" error(1 answer)Closed 9 years ago.We are running a Tornado 3.0 service on a RedHat OS and getting the …

How to check if an RGB image contains only one color?

Im using Python and PIL.I have images in RGB and I would like to know those who contain only one color (say #FF0000 for example) or a few very close colors (#FF0000 and #FF0001).I was thinking about us…

python requests and cx_freeze

I am trying to freeze a python app that depends on requests, but I am getting the following error:Traceback (most recent call last):File "c:\Python33\lib\site-packages\requests\packages\urllib3\ut…