英文:
"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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论