“TypeError: ‘int’对象不可订阅” 在Python中

huangapple go评论64阅读模式
英文:

"TypeError: 'int' object is not subscriptable" in python

问题

def get_hidden_card(credit_number, star_count=4):
    short_credit_number = str(credit_number[12:16])
    return str('*' * star_count + short_credit_number)

print(get_hidden_card(2034399002125581))
英文:
def get_hidden_card(credit_number, star_count=4):
    short_credit_number = str(credit_number[12:16])
    return str('*' * star_count + short_credit_number)


print(get_hidden_card(2034399002125581))

As i understand python don't wanna work because he think that str(credit_number[12:16]) is int type, how can i fix it?

The error is:

    Traceback (most recent call last):
  File "C:\Users\Ivan\PycharmProjects\pythonProject\main.py", line 10, in <module>
    print(get_hidden_card(2034399002125581))
  File "C:\Users\Ivan\PycharmProjects\pythonProject\main.py", line 4, in get_hidden_card
    short_credit_number = str(credit_number[12:16])
TypeError: 'int' object is not subscriptable

Process finished with exit code 1

答案1

得分: 0

这应该解决了你的问题

def get_hidden_card(credit_number, star_count=4):
    short_credit_number = str(credit_number)[12:16]
    return str('*' * star_count + short_credit_number)

print(get_hidden_card(2034399002125581))
英文:

This is must solve your issue

def get_hidden_card(credit_number, star_count=4):
        short_credit_number = str(credit_number)[12:16]
        return str('*' * star_count + short_credit_number)
    
    
    print(get_hidden_card(2034399002125581))

huangapple
  • 本文由 发表于 2023年2月19日 18:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定