我在默認的IDE中運行python 3。AttributeError:'int'對象沒有屬性來自用戶輸入的'isdigit'
這裏是我的代碼:
def print_():
f = open("height.txt","r")
content = f.read()
print(content)
f.close()
main()
def main():
name = str(input("What is your name?"))
data = int(input("How tall are you? (CM)"))
if data.isdigit() == True:
print("\n")
elif data.isdigit() == False:
print("Must be a number!")
main()
# a+ is read only mode
f = open("height.txt","a+")
f.write(name)
f.write(str(data))
f.write("cm, ")
f.close()
data_1 = str(input("1 = View Contents. Other = Quit"))
if data_1 == '1':
print_()
else:
print("Exiting")
main()
我的錯誤是 - if data.isdigit()== True: AttributeError:'int'object has no attribute'isdigit' –
似乎這個程序可能會遇到遞歸問題。如果數據不是int,然後寫入height.txt,則調用main函數。我認爲你想在main調用之後殺掉函數...所以。打印(「必須是數字」)main()返回 – mattsap
好吧,我明白了!非常感謝你:) –