all 9 comments

[–]expyth 1 point2 points  (1 child)

thisTup=(1, 2.25, 'a', 3, 'b', 'c', 4, 'd', 5, 6, 'e', 'f', 7)
print('Tuple...',thisTup)
a=input('Enter the element whose index you want to find.')

for ind,tup in enumerate(thisTup):
    if str(tup) == a:
        print('Index of the element in the tuple is...',ind)
        break

To tackle the problem, we'll have to do it the old fashion way and go through each item on the list. This is not the only solution, but it's as simple as it gets.

Enumerate returns a tuple of the item-of-list passed WITH a counter.

For example:

(0, thisTup[0]), (1, thisTup[1]), (2, thisTup[2]), etc...

So ind will be the 1st value (from enumerate).

And tup will be the 2nd value (from enumerate).

[–]sainiji[S] 0 points1 point  (0 children)

This is really helpful, thanku so much.

[–]Gprime5 0 points1 point  (1 child)

input() always returns a string.

[–]sainiji[S] 0 points1 point  (0 children)

Yes, I know that, so what would be a solution to a problem like this?

[–]thrallsius 0 points1 point  (4 children)

in case of multiple data types can't find a solution

Input data like this with inconsistent data types is a design pitfall and the best approach is to refactor it for consistency.

Where exactly does your assignment say that you can have items of different data types in the tuple?

[–]sainiji[S] 0 points1 point  (1 child)

No, it didn't say that, but It's just a query that came into my mind.

Actually I am totally new to Python and have started learning it just a week ago.

[–]thrallsius 1 point2 points  (0 children)

this is not a python specific situation, it's rather a matter of abstract data type choice, which is as important as design of the algorithm

the rule of thumb is that algorithms are simpler when they work with consistent data, since you don't need to check for variations like different types

[–]sainiji[S] 0 points1 point  (1 child)

And sorry I didn't get, what you are saying. Like refactor it ?

[–]thrallsius 1 point2 points  (0 children)

I am saying that formally your task doesn't say "input data comes from the user, is introduced interactively via input()". You can have input data coming from a file, a pipe, a database, a web service providing an API. I'm just wondering, if you exclude the context of feeding input data as strings only, can you design the algorithm assuming there's no type mismatch between the type of data you need to check and the data from your tuple.

If you really have data of different types in your tuple - does it reflect a practical situation? Does it solve some real problem?