簡體   English   中英

Python數組拆包錯誤:需要超過3個值

[英]Python array unpacking error: need more than 3 values

我對Python相當陌生,正在嘗試比較存儲在數組中的某些值。 當我運行代碼時,出現錯誤:ValueError:需要多個3個值才能解壓。 我的代碼是:

walls = ['300', '300', '300', '500']
for a, b, c, d in walls:
    if int(a) <= x <= int(c):
            if int(b) <= y <= int(d):

引發錯誤的是“ for”行。 我知道在線有解決方案,但都不是針對陣列的,因此非常感謝您的幫助。

您的代碼應變為:

walls = ['300', '300', '300', '500']
a, b, c, d = walls # this line replaces the `for` loop
if int(a) <= x <= int(c):
    if int(b) <= y <= int(d):
        pass # or do something...

該代碼的初始版本遍歷walls每個項目,並嘗試將它們分配給您的四個變量。

因此,從本質上講, for循環的每次迭代都嘗試執行以下操作:

a, b, c, d = '300'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM