int object is not iterable?

前端 未结 11 926
天命终不由人
天命终不由人 2020-12-01 11:19
inp = int(input(\"Enter a number:\"))

for i in inp:
    n = n + i;
    print (n)

... throws an error: \'int\' object is not iterable<

11条回答
  •  被撕碎了的回忆
    2020-12-01 11:45

    As ghills had already mentioned

    inp = int(input("Enter a number:"))
    
    n = 0
    for i in str(inp):
        n = n + int(i);
        print n
    

    When you are looping through something, keyword is "IN", just always think of it as a list of something. You cannot loop through a plain integer. Therefore, it is not iterable.

提交回复
热议问题