简体   繁体   English

无法分配给函数调用 Error-Python

[英]can't assign to function call Error-Python

I'm working on a project to create a postal bar code out of an inputted 5 digit zipcode.我正在做一个项目,用输入的 5 位邮政编码创建邮政条形码。 When I run the code, I get an error that says "can't assign to function call".当我运行代码时,我收到一条错误消息,提示“无法分配给函数调用”。 What is wrong with the code, and how do I fix it?代码有什么问题,我该如何解决?

zipcode = input("What is your 5 digit zipcode?")
s = zipcode.split(",")

def barcode(a):                                                        
    if a==0:
        print("||:::") 
    elif a==1:
        print(":::||")
    elif a==2:
        print("::|:|")
    elif a==3:
        print("::||:")
    elif a==4:
        print(":|::|")
    elif a==5:
        print(":|:|:")
    elif a==6:
        print(":||::")
    elif a==7:
        print("|:::|")
    elif a==8:
        print("|::|:")
    elif a==9:
        print("|:|::")

for barcode(a) in s:
    print(barcode(a))

Your error is here:你的错误在这里:

for barcode(a) in s:

It's invalid syntax because the name bound in a for loop has to be a python identifier.这是无效的语法,因为在 for 循环中绑定的名称必须是 python 标识符。

You were probably trying for something like this instead:您可能正在尝试这样的事情:

for the_zipcode in s:
    print(barcode(the_zipcode))

I am fairly certain your problem is your use of s=zipcode.split(",") .我相当肯定你的问题是你使用s=zipcode.split(",") What that does is split the string that is put in by the user (if you're using python 3) into an array of strings, where each element is delimited by a comma.这样做是将用户输入的字符串(如果您使用的是 python 3)拆分为字符串数组,其中每个元素由逗号分隔。 For example:例如:

'11111'.split(',') # ['11111']
'11111,12345'.split(',') # ['11111', '12345']

That is almost certainly not what you want, since you're asking the user to input a 5-digit zip code.这几乎肯定不是您想要的,因为您要求用户输入一个 5 位数的邮政编码。 If you just use the input directly, I think you'll get what you want.如果你直接使用输入,我想你会得到你想要的。

That is:那是:

zipcode=input("What is your 5 digit zipcode?")
# . . .
for digit in zipcode:
  print(barcode(digit)) 

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

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