AttributeError:'int'对象没有来自用户输入的属性'isdigit'

时间:2015-11-19 16:47:41

标签: python ide

我在默认IDE中运行python 3。

这是我的代码:

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()

2 个答案:

答案 0 :(得分:1)

您正在尝试检查isdigit()您已投放到int的数据。将其作为str阅读,然后在您检查了它之后进行投射:

def main():     
    name = str(input("What is your name?"))
    data = str(input("How tall are you? (CM)"))

    while !data.isdigit():
        print("Must be a number!")
        data = str(input("How tall are you? (CM)"))
        print("\n")

    data = int(data)
    print("\n")

这个循环将继续请求一个int,直到提供一个,然后将其转换为int

答案 1 :(得分:0)

问题是isdigit对字符串进行操作;但是你已经将数据对象转换为int:data = int(输入(“你有多高?(CM)”))。如果要使用isdigit函数,则应该将数据设为字符串。 data = str(输入(“...”))