Python Forum

Full Version: How to make a variable contain multiple values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Lets say I want to create a little program that asks for a user input of 1 through 10.
If that input is 1,3,5,7,9 the user wins
if the that input is 2,4,6,8 the user loses

I cannot figure out how to make a variable that contains all those integers.

I started with this:

input_value = input("Enter value 1 through 10:")
input_value = int(input_value)
win_num = [1, 3, 5, 7, 9]

if input_value == win_num:
   print("you win")
else:
   print("you lose")
I get you lose no matter the value I enter.
One number at a time or all on one line?
I do not want to make multiple lines for multiple variables.
No, input (type) all the numbers at once or have multiple inputs so the user enters numbers one at a time?
User gets one try.

Enter value 1 through 10: 1
you win


Enter value 1 through 10: 2
you lose
I am confused. The user inputs 1 number. Test if this is IN the list of winning numbers. If it is, they win, otherwise they lose. Other than one line that is doing the checking, this is what your program does.

So is your question really "How do I check if a number is in a list?"
the program is just a "playing around" thing to get my need across. So then with you question yes, I want the user to input a number and check if that is in a list of numbers predefined by me.
Other than a tiny bug your program already does that. The input value is obviously never going to equal [1, 3, 5, 7, 9]. One is an integer and the other is a list. But there are comparisons to find if a number is IN a list.

My confusion is from the topic. "How to make a variable contain multiple values". Then you provide an example program that does exactly that:
win_num = [1, 3, 5, 7, 9]
That is a variable that contains multiple values. That was why I was asking dumb questions about input. "Where is he trying to create a variable that contains multiple values other than the one variable he already created that contains multiple values?"

What is the question again?
Use the in operator in your if statement

And also, please use proper code tags while posting a thread
i wrote something like this, i hope it is what you wanted:

numbers_list = [1, 3, 5, 7, 9]
choice = int(input('please choose a number:'))

if choice in numbers_list:
        print('you just won !')
        
else:
    print('you just lost amigo, try again !')
Pages: 1 2