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

7
  • 1
    Does this answer your question? Python - TypeError: 'int' object is not iterable Commented Sep 26, 2021 at 14:48
  • hmm, i still don't get it, because it should be written with object oriented programming... Commented Sep 26, 2021 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. Commented Sep 26, 2021 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.. Commented Sep 26, 2021 at 14:53
  • Please remove the image - share code only. Commented Sep 26, 2021 at 14:55

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 [[...]]
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! ;)
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.