0

So I'm learning about classes and objects in Python 3, but there's an error in my code ('int' object is not iterable)

Error

class student:
    name = 0
    gpa = 0
    def countGpa(self):
        a = max(self.gpa)
        print(a)

def main():
    objek = student()
    n = int(input("n: "))
    for i in n:
        objek.name = int(input("name: "))
        objek.gpa = int(input("gpa: "))
    objek.countGpa()
main()

I have no idea how to create an array + OOP

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 1
    Does this answer your question? [Python - TypeError: 'int' object is not iterable](https://stackoverflow.com/questions/19523563/python-typeerror-int-object-is-not-iterable) – user438383 Sep 26 '21 at 14:48
  • hmm, i still don't get it, because it should be written with object oriented programming... – tinymermaid Sep 26 '21 at 14:50
  • There are lots of things wrong here. For a start you create a single instance of `student`, but you seem to be asking for many names. This implies that you want lots of instances. That would make more sense when it comes time to work out the `max()` gpa. – quamrana Sep 26 '21 at 14:50
  • then, how to create array with oop? before i started with ```objek.name[i]= str(input("name: "))``` but it doesn't work here.. – tinymermaid Sep 26 '21 at 14:53
  • Please remove the image - share code only. – balderman Sep 26 '21 at 14:55
  • okay, i'd remove it wait... – tinymermaid Sep 26 '21 at 15:06
  • You've just changed your question to something else. We are not your personal development system. This site is for asking a specific question and receiving a specific answer. You already have two answers to your original problem. You should accept one of those and move on to the next problem. (There are lots in your post to keep you busy). – quamrana Sep 26 '21 at 15:18

2 Answers2

1

Only regarding your error: Convert for i in n: to for i in range(n):

DevConcepts
  • 174
  • 8
  • Is it true like this? ```listGpa = [] class student: name = 0 gpa = 0 def countGpa(self): self.gpa = listGpa listGpa.append(self.gpa) print(listGpa) def main(): objek = student() n = int(input("n: ")) for i in range(n): objek.name = str(input("name: ")) objek.gpa = int(input("gpa: ")) objek.countGpa() main()``` but why i can't print my array , output is like this ```[[...]]``` – tinymermaid Sep 26 '21 at 15:03
  • The error has been fixed. If you want your code to work properly, you need to put some of your own effort into this as well. At the very beginning it is really frustrating to learn coding, but keep it up! ;) – DevConcepts Sep 26 '21 at 16:15
0

'for i in n:' doesn't work like you want. The variable 'n' is an integer and You want an array. You want 'for i in range(n):' which creates an array of the numbers one through n.