Python Forum
How to make a variable contain multiple values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to make a variable contain multiple values (/thread-26334.html)

Pages: 1 2


How to make a variable contain multiple values - stevenjwilliams83 - Apr-28-2020

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.


RE: How to make a variable contain multiple values - deanhystad - Apr-28-2020

One number at a time or all on one line?


RE: How to make a variable contain multiple values - stevenjwilliams83 - Apr-28-2020

I do not want to make multiple lines for multiple variables.


RE: How to make a variable contain multiple values - deanhystad - Apr-28-2020

No, input (type) all the numbers at once or have multiple inputs so the user enters numbers one at a time?


RE: How to make a variable contain multiple values - stevenjwilliams83 - Apr-28-2020

User gets one try.

Enter value 1 through 10: 1
you win


Enter value 1 through 10: 2
you lose


RE: How to make a variable contain multiple values - deanhystad - Apr-28-2020

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?"


RE: How to make a variable contain multiple values - stevenjwilliams83 - Apr-28-2020

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.


RE: How to make a variable contain multiple values - deanhystad - Apr-28-2020

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?


RE: How to make a variable contain multiple values - pyzyx3qwerty - Apr-29-2020

Use the in operator in your if statement

And also, please use proper code tags while posting a thread


RE: How to make a variable contain multiple values - astral_travel - Apr-29-2020

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 !')