本文介紹了錯誤:'int' 對象不可下標 - Python的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試一段簡單的代碼,獲取某人的姓名和年齡,并在他們 21 歲時告訴他/她...不考慮負面因素和所有這些,只是隨機的.
I was trying a simple piece of code, get someone's name and age and let him/her know when they turn 21... not considering negatives and all that, just random.
我不斷收到這個 'int' object is not subscriptable
錯誤.
I keep getting this 'int' object is not subscriptable
error.
name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
x = 0
int([x[age1]])
twentyone = 21 - x
print "Hi, " + name1+ " you will be 21 in: " + twentyone + " years."
推薦答案
問題出在行,
int([x[age1]])
你想要的是
x = int(age1)
您還需要將 int 轉換為字符串以供輸出...
You also need to convert the int to a string for the output...
print "Hi, " + name1+ " you will be 21 in: " + str(twentyone) + " years."
完整的腳本看起來像,
name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
x = 0
x = int(age1)
twentyone = 21 - x
print "Hi, " + name1+ " you will be 21 in: " + str(twentyone) + " years."
這篇關于錯誤:'int' 對象不可下標 - Python的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!