Home>

I get an error TypeError: 'int' object is not iterable. I want to display the average score by clicking on several buttons.

import tkinter as tk
from tkinter import *
win= Tk ()
win.geometry ('450x250')
win.resizable (0,0)
win.title ('Grade Average')
def btn_click (item):
    global expression
    expression= list (map (int, item)) /len (item)
    input_text.set (expression)
def bt_clear ():
    global expression
    expression= ""
    input_text.set ("")
expression= ""
input_text= StringVar ()
myLabel1= Label (win, textvariable= input_text, font= ("Montserrat", 15))
myLabel1.pack ()
one= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "1", width= 4, command= lambda: btn_click (1))
one.pack ()
two= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "2", width= 4, command= lambda: btn_click (2))
two.pack ()
three= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "3", width= 4, command= lambda: btn_click (3))
three.pack ()
four= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "4", width= 4, command= lambda: btn_click (4))
four.pack ()
five= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "5", width= 4, command= lambda: btn_click (5))
five.pack ()
win.mainloop ()

In the btn_click item argument, you pass a number in different places. And then you try to use it as len (item). It is clear that this does not work.

Roman Konoval2021-11-11 15:19:35

expression= list (map (int, item)) /len (item) you pass a number here!

Андрей Крузлик2021-11-11 15:35:08
  • Answer # 1

    to functionbtn_click (item)you need to pass a list for example['1', '2', '3']

    expression= sum (map (int, item)) /len (item)
    

    sum (map (int, item))-mapconverts each element to a number, andsumwill calculate the amount. Well andlen (item)divide by number of elements

    import tkinter as tk
    from tkinter import *
    win= Tk ()
    win.geometry ('450x250')
    win.resizable (0, 0)
    win.title ('Grade Average')
    def btn_click (item):
        global expression
        expression= sum (map (int, item)) /len (item)
        input_text.set (expression)
    def bt_clear ():
        global expression
        expression= ""
        input_text.set ("")
    expression= ""
    input_text= StringVar ()
    myLabel1= Label (win, textvariable= input_text, font= ("Montserrat", 15))
    myLabel1.pack ()
    mylist= []
    one= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "1", width= 4,
                 command= lambda: mylist.append (1))
    one.pack ()
    two= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "2", width= 4,
                 command= lambda: mylist.append (2))
    two.pack ()
    three= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "3", width= 4,
                   command= lambda: mylist.append (3))
    three.pack ()
    four= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "4", width= 4,
                  command= lambda: mylist.append (4))
    four.pack ()
    five= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "5", width= 4,
                  command= lambda: mylist.append (5))
    five.pack ()
    sr= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "Average", width= 20,
                command= lambda: btn_click (mylist))
    sr.pack ()
    win.mainloop ()
    

    He doesn't calculate the GPA

    JustChickNugget2021-11-11 16:06:07

    fixed! "I want to display my average score by clicking on a few buttons."

    Андрей Крузлик2021-11-11 16:16:20
  • Answer # 2

    to functionbtn_click (item)you need to pass a list for example['1', '2', '3']

    expression= sum (map (int, item)) /len (item)
    

    sum (map (int, item))-mapconverts each element to a number, andsumwill calculate the amount. Well andlen (item)divide by number of elements

    import tkinter as tk
    from tkinter import *
    win= Tk ()
    win.geometry ('450x250')
    win.resizable (0, 0)
    win.title ('Grade Average')
    def btn_click (item):
        global expression
        expression= sum (map (int, item)) /len (item)
        input_text.set (expression)
    def bt_clear ():
        global expression
        expression= ""
        input_text.set ("")
    expression= ""
    input_text= StringVar ()
    myLabel1= Label (win, textvariable= input_text, font= ("Montserrat", 15))
    myLabel1.pack ()
    mylist= []
    one= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "1", width= 4,
                 command= lambda: mylist.append (1))
    one.pack ()
    two= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "2", width= 4,
                 command= lambda: mylist.append (2))
    two.pack ()
    three= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "3", width= 4,
                   command= lambda: mylist.append (3))
    three.pack ()
    four= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "4", width= 4,
                  command= lambda: mylist.append (4))
    four.pack ()
    five= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "5", width= 4,
                  command= lambda: mylist.append (5))
    five.pack ()
    sr= Button (win, font= ("Montserrat", 12), bg= '# dadada', text= "Average", width= 20,
                command= lambda: btn_click (mylist))
    sr.pack ()
    win.mainloop ()
    

    He doesn't calculate the GPA

    JustChickNugget2021-11-11 16:06:07

    fixed! "I want to display my average score by clicking on a few buttons."

    Андрей Крузлик2021-11-11 16:16:20