Python Forum
How to make a variable contain multiple values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a variable contain multiple values
#1
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.
Reply
#2
One number at a time or all on one line?
Reply
#3
I do not want to make multiple lines for multiple variables.
Reply
#4
No, input (type) all the numbers at once or have multiple inputs so the user enters numbers one at a time?
Reply
#5
User gets one try.

Enter value 1 through 10: 1
you win


Enter value 1 through 10: 2
you lose
Reply
#6
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?"
Reply
#7
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.
Reply
#8
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?
Reply
#9
Use the in operator in your if statement

And also, please use proper code tags while posting a thread
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#10
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 !')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assignment to make a multiple choice game blacklight 1 2,117 Aug-14-2020, 02:37 AM
Last Post: Larz60+
  Passing multiple values from template to template card51shor 0 1,678 Jun-18-2020, 06:44 PM
Last Post: card51shor
  5 Variable- Multiple Boolean Code cf25 1 2,326 Dec-17-2019, 03:54 PM
Last Post: ichabod801
  Extracting variable values from labels on csv file using Python Laura 1 2,181 Nov-12-2018, 06:54 PM
Last Post: ichabod801
  How to make a dynamic variable based on input loltylerdowney 3 3,711 Nov-30-2017, 09:49 PM
Last Post: loltylerdowney

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020