Home > Back-end >  return and multiple assignment (python)
return and multiple assignment (python)

Time:06-03

I have been trying to make code that prints multiple by using the define function.

first, this code is an answer and it uses tuple.

Answer Code

def multiples(n, m):
    tup=()
    for i in range(1,m 1):
        tup=tup (i*n,)
    return tup
    
r1,r2,r3,r4=multiples(3,4)
print(r1,r2,r3,r4)
r1,r2,r3,r4,r5=multiples(2,5)
print(r1,r2,r3,r4,r5)

and I was trying another way to make this code. so this is my first try code.

Code 1

def multiples(n, m):
    for i in range(1,m 1):
        print(n*i,end='')

r1,r2,r3,r4=multiples(3,4)
print(r1,r2,r3,r4)
r1,r2,r3,r4,r5=multiples(2,5)
print(r1,r2,r3,r4,r5)

and I ran the code, it brought me this Error.

TypeError: cannot unpack non-iterable NoneType object

I googled it and I realized I didn't use iterable things. so I tried again.

Code 2

def multiples(n, m):
    l1=[]
    for i in range(1,m 1):
        l1=l1*(n*i)
    return l1
        
r1,r2,r3,r4=multiples(3,4)       # line 7
print(r1,r2,r3,r4)
r1,r2,r3,r4,r5=multiples(2,5)
print(r1,r2,r3,r4,r5)

and I got this error at line 7.

ValueError: not enough values to unpack (expected 4, got 0)

I want to know

1. why there are no values ?

2. Can't I run this code successfully without using iterable object?

3. Can I use str type for replacing another type of iterable object (like I changed tuple to list ) in that code?

Thank you for answering my questions.

CodePudding user response:

you can try appending to list like :

def multiples(n, m):
    l1=[]
    for i in range(1,m 1):
        l1 =[(n*i)]             # or l1.append(n*i) 
    return l1
        
r1,r2,r3,r4=multiples(3,4)       # line 7
print(r1,r2,r3,r4)
r1,r2,r3,r4,r5=multiples(2,5)
print(r1,r2,r3,r4,r5)

CodePudding user response:

In Code 1, you never returned a value (no return statement in your function).

In Code 2, you create an empty list l1 = [] and then multiplies it by itself. [4] * 3 evaluates to [4,4,4] right ? What do you think [] * 3 evaluates to ?

For your last question, str is an iterable type. As long as you can represents your results with characters, then yes, you can swap list to str.

However, it will requires a little machination since the number 8 is not the same that the character '8' :-)

CodePudding user response:

  1. Because you return one list, and you expect separate values (variables).
  2. you can do that.

Just use this code:

def multiples(n, m):
    l1=[]
    for i in range(1,m 1):
        l1.append(n*i)
    return l1
        
l1=multiples(3,4)       # line 7
print(l1)
l1=multiples(2,5)
print(l1)

CodePudding user response:

Note that in "Code 1", you're printing and have no return statement so your function implicitly returns None and None is a single value, not an iterable, hence the error.

CodePudding user response:

  1. you multiply an empty list repeatedly, and it is still an empty list after each update.
  2. yes.
  3. yes, the list is completely allowed, but str may have unexpected results.
  • Related