Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find address of element in tuple of diff data types

i have tuple a=("Orange", [10,20,30], (24,25,66))
when user inputs "O" it shows indexes right 0 0 good ,
but when i enter 10 nothing happens why? it must show 1 0

a=("Orange", [10,20,30], (24,25,66))
q=input()
for i in a:
    if isinstance(i,str):
        if q in i:
            print(a.index(i),i.index(q))
    elif isinstance(i,tuple) or isinstance(i,list):
        if q in i:
            print(a.index(i),i.index(int(q)))

I realized my mistake thank y guys
1 more question then what if a=("Orange", ["10",20,30], (24,25,66))
how can i find indexes of "10" as string

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you enter 10, what really happens is that q is assigned to "10", ie a string of two characters.

Your tuple a contains a list of numbers: [10, 20, 30], and all of the elements are integers and none of them are strings.

You could change the elif to go looking for and integer like this:

    ...
    elif isinstance(i,tuple) or isinstance(i,list):
        num = int(q)
        if num in i:
                print(a.index(i), i.index(num))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading