r/learnpython • • Nov 07 '24

How do I compare 2 variables, where one variable contains 5 values?

This is basically what it boils down to:

How do I see if variable x is equal to variable y where y contains 5 numbers? (For example 1,2,3,4,5)

0 Upvotes

13 comments sorted by

12

u/DangerDinks Nov 07 '24

Do you mean that you want to see if variable x is contained within variable y?

If so, you can do

if x in y:
  # some_logic

1

u/Brick_Bronze8 Nov 07 '24

Yes. In the specific program x is an input while y is randomly generated. And here specifically we want to see if x is one of the 5 randomly generated numbers in y

3

u/MrSirSchmeckle Nov 07 '24

Above would be best but if you wanted to break it down, you could use a for loop to iterate through list y to see if any number is equal to x.

2

u/copperfoxtech Nov 07 '24 edited Nov 07 '24
# random module for random numbers
import random

# Ask user for input and cast it to an integer.
x = int(input('Pick a number between 1 and 10: ))
# Generate a random list of five numbers from 1 to 10.
y = [random.randrange(1, 10 + 1) for _ in range(5)]

# print so you can see the random list.
print(y)

# Conditional asking if x is inside the list y
if x in y:
    print("it's in there!")
else:
    print("it's NOT in there!")

1

u/copperfoxtech Nov 07 '24

Another solution for type checking, never trust a users input!

import random

# Infinite loop solution to keep asking the user to input the correct value
while True:
    try:
        # Here we ask them to input
        user_input = input('Enter a number between 1 and 10: ')
        # We attempt to cast it to an integer
        x = int(user_input)
        # we ask if it is in the range of 1 - 10
        if 1 <= x <= 10:
            break  # Exit loop if the input is valid
        else:
            # Warning of sorts if not in the range of 1 - 10
            print("Number out of range. Try again.")
    except ValueError:  # if they entered something that is not a number
        print("Please enter a valid integer.")

y = [random.randint(1, 10 + 1) for _ in range(5)]

print(y)

if x in y:
    print("It's in there!")
else:
    print("It's NOT in there!")

6

u/shiftybyte Nov 07 '24

Does x also contain 5 numbers? Then you can do that with equals sign:

>>> x = [1,2,3,4,5]
>>> y = [1,2,3,4,5]
>>> x == y
True

Is x a single number? then it can't be equal to 5 numbers now can it?

Perhaps you want to ask if x is contained in a list of 5 numbers?

>>> x = 5
>>> y = [1,2,3,4,5]
>>> x in y
True

1

u/Brick_Bronze8 Nov 07 '24

Yes the program is trying to find out if x is one of the 5 numbers in y

2

u/zefciu Nov 07 '24

If y is big, then using a set instead of list might be a good choice.

3

u/danielroseman Nov 07 '24

What is x? What do you want to compare? Why wouldn't x == y work?

1

u/VipeholmsCola Nov 07 '24

You need to be more specific.

If the first variable can be either of the other 5 values, then check 'if a in (1,2,3,4,5)'

Otherwise, you need to make sure that the first metric is comparable to the second.