Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert tuple to int in Python

I'm brand new at python, and didn't understand the other answers for this question. Why when I run my code, does int(weight[0]) not convert variable "weight" into a integer. Try your best to dumb it down because I'm really new and still don't quite understand most of it. Here is the relevant section of my code

weight = (lb.curselection())
    print ("clicked")
    int(weight[0])
    print (weight)
    print (type(weight))

and heres my code for this script

lb = Listbox(win, height=240)
lb.pack()
for i in range(60,300):
    lb.insert(END,(i))
def select(event):
    weight = (lb.curselection())
    print ("clicked")
    int(weight[0])
    print (weight)
    print (type(weight))
lb.bind("<Double-Button-1>", select)

Thanks

When I run the code, it comes up with TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple' and I want it instead to convert the "weight" variable into a integer, so I can use it for math operations.

Full Traceback:Traceback (most recent call last): File "C:\Users\Casey\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "C:/Users/Casey/AppData/Local/Programs/Python/Python36-32/s.py", line 11, in select int(weight) TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

like image 608
casey ryan Avatar asked Aug 02 '17 15:08

casey ryan


1 Answers

what you're looking for is

weight = int(weight[0])

int is a function that returns an integer, so you have to assign that return to a variable.

if what you're looking for is to reassign the variable weight with the value of its first record, that code should work for you.

If the item is already an integer then the int call might be redundant, you might be able to get it with just

weight = weight[0]
like image 68
Stael Avatar answered Oct 08 '22 06:10

Stael