[英]Converting integer to bits: TypeError: string indices must be integers - Python3
[英]TypeError: string indices must be integers - Python3
問題
所以,我的問題是我遇到了TypeError: string indices must be integers
這行的TypeError: string indices must be integers
: userCHR += str(user[i])
,但是代碼對我來說很好。 有人能幫我嗎?
Python代碼
user = input("Please enter a string: ")
user = user.upper()
userCHR = ""
for i in user:
userCHR += str(user[i])
print(userCHR)
如果我沒看錯,則i已經是一個字符串。 也許您可以嘗試:
for i in user:
userCHR += i
for循環在Python中返回字符串中的字符,而不是整數位置。
userCHR += str(user[i])
userCHR += i
user = input("Please enter a string: ")
user = user.upper()
userCHR = ""
for i in user:
userCHR += i
print(userCHR)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.