Error using defined function: cannot unpack non-iterable NoneType object

Asked 2 years ago, Updated 2 years ago, 354 views

Define the function as follows and write the code for the quotient and remainder of n/k.

defquotient_and_remainder(n,k):
         quotient==n//k
         remainder == n%k
         print(quontient, reminder)

quote,remainder=quotient_and_remainder(10,3)
print(quotient)
print(remainer)

The error "cannot unpack non-iterable NoneType object" does not work.

  • Whether the function you defined is returned (printed part) or not
  • quotient,remaininder=quotient_and_remaininder(10,3) intended to display the quotient and remainder separately.
  • Where should I modify to make it work

Let me ask you a question about

Thank you for your cooperation.

python

2022-09-30 22:01

1 Answers

By the way, as with the previous question, I don't think I have mastered basic knowledge.
If you do that, you tend to stumble and ask questions repeatedly if you do something a little new.

It is recommended that you try to find a suitable course or course that you can learn in order.

Also, there seems to be a lot of typo errors when asking questions, so please review them.
The question does not reproduce the error in the question.

The answers are as follows:

  • Whether the function you defined is returned (printed) or not

print is the display and does not specify the return value of the function.
Define the return value for the function by return.

It's a little long, but please read the instructions below on how to define the linked function.
At the end of the chapter, there is a way to return the return value.
4.7.Define a function

The return statement is described below.
7.6.return statement

Like many other programming languages, if you write an immediate value or variable after the return, that is the return value (the expression list can also be the return value as described in the description).If it's just a return, or if it's not a return, it's a None return.
Then, if you arrange multiple values separated by commas, as shown in the answer to the next item, it appears that you have returned multiple return values.(It's actually returning a single value as a tuple.)but)

  • ...is it intended to display the quotient and remainder separately

This part is correct.

This article will help you find out how to return multiple return values and how to receive them.
How to return multiple return values in Python's function

Other articles like this
How to define Python function returns with or without values and multiple differences
[Python] How to write the return value of the function (multiple)

If more than one return value is specified, as shown in the last introduction, you have three choices: Receive results with only one variable, receive as many variables as the number of returns specified, or ignore the result without doing anything.
If the correct return method is implemented, the question is correct.

The return value was returned incorrectly (I didn't specify the return value in the first place), so when only one None was returned, two variables were specified on the receiving side, so I think there was an error.

  • Where should I modify to make it work

It's better to avoid this kind of listening because it's close to what's called a "round throw."
You should consider how to ask questions and how to write them.
I won't give you any specific code, but it will work if you modify the return method of typo and return value.


2022-09-30 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.