简体   繁体   English

返回和多重赋值(python)

[英]return and multiple assignment (python)

I have been trying to make code that prints multiple by using the define function.我一直在尝试使用 define 函数制作打印多个的代码。

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代码 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 TypeError:无法解压不可迭代的 NoneType 对象

I googled it and I realized I didn't use iterable things.我用谷歌搜索了它,我意识到我没有使用可迭代的东西。 so I tried again.所以我又试了一次。

Code 2代码 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.我在第 7 行得到了这个错误。

ValueError: not enough values to unpack (expected 4, got 0) ValueError:没有足够的值来解包(预期 4,得到 0)

I want to know我想知道

1. why there are no values ? 1.为什么没有价值观?

2. Can't I run this code successfully without using iterable object? 2. 如果不使用可迭代对象,我不能成功运行这段代码吗?

3. Can I use str type for replacing another type of iterable object (like I changed tuple to list ) in that code? 3. 我可以使用 str 类型来替换该代码中的另一种类型的可迭代对象(比如我将 tuple 更改为 list )吗?

Thank you for answering my questions.谢谢你回答我的问题。

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)

In Code 1 , you never returned a value (no return statement in your function).Code 1中,您从未返回值(函数中没有return语句)。

In Code 2 , you create an empty list l1 = [] and then multiplies it by itself.代码 2中,您创建一个空列表l1 = [] ,然后将其与自身相乘。 [4] * 3 evaluates to [4,4,4] right ? [4] * 3计算结果为[4,4,4]对吗? What do you think [] * 3 evaluates to ?你认为[] * 3的结果是什么?

For your last question, str is an iterable type.对于你的最后一个问题, str一个可迭代的类型。 As long as you can represents your results with characters, then yes, you can swap list to str.只要您可以用字符表示结果,那么可以,您可以将列表交换为 str。

However, it will requires a little machination since the number 8 is not the same that the character '8' :-)但是,这需要一点技巧,因为数字8与字符'8'不同:-)

  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)

请注意,在“代码 1”中,您正在打印并且没有return语句,因此您的函数隐式返回None并且None是单个值,而不是可迭代的,因此会出现错误。

  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.是的,列表是完全允许的,但是 str 可能会有意想不到的结果。

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

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