How can I make a raw_input choice based on a list provided give me a correct result when chosen?

707 Views Asked by At

What I want is, based on a raw_input question and list provided I want a print result related to an item chosen from a list( i.e football, rugby, basketball etc.)

I would be grateful if someone could help me with it

balls = ['Basketball', 'Football', 'Golf', 'Tennis', 'Rugby']        
others = ['Hockey', 'Chess', 'Poker']

raw_input("Please chose a sport you want to play: ")

for x,y in zip(balls,others):
    if x == balls.choice():
        print "You have chosen a sport with balls!"
    else:
        print "You have chosen others"    `
4

There are 4 best solutions below

0
On

You could just assign the raw_input to a variable and check if the variable is in balls or not in balls:

sport = raw_input("Please chose a sport you want to play: ")
if sport in balls: 
    print "You have chosen a sport with balls!" 
else: 
    print "You have chosen others"
1
On

No idea, why you are zipping two lists.

balls = ['Basketball', 'Football', 'Golf', 'Tennis', 'Rugby']
others = ['Hockey', 'Chess', 'Poker']


sport = raw_input("Please chose a sport you want to play: ")

if sport in balls:
    print ("You have chosen a sport with balls!")
if sport in others:
    print ("You have chosen other")

Output:

C:\Users\dinesh_pundkar\Desktop>python gz.py
Please chose a sport you want to play: Hockey
You have chosen other

C:\Users\dinesh_pundkar\Desktop>python gz.py
Please chose a sport you want to play: Football
You have chosen a sport with balls!

C:\Users\dinesh_pundkar\Desktop>
0
On

There is no need to loop over the options, you can use python's excellent in keyword. You also need to save the result of raw_input somewhere so you can compare it:

balls = ['Basketball', 'Football', 'Golf', 'Tennis', 'Rugby']        
others = ['Hockey', 'Chess', 'Poker']

user_input = raw_input("Please chose a sport you want to play: ")

if user_input in balls:
  print "You have chosen a sport with balls!"
elif user_input in others:
  print "You have chosen others"
else:
  print "You have entered something else"
0
On

You can get the index of item dynamically:

choice = raw_input("Please chose a sport you want to play: ")
if choice in balls:
    print (balls[balls.index(choice)])