2016-12-22 47 views
0

我要的是,根據所提供 我想與從列表中選擇(即足球,橄欖球,籃球等)的項目的打印結果的問題的raw_input和列表如何根據提供的列表創建一個raw_input選項選擇時給我一個正確的結果?

我將不勝感激,如果有人能夠幫我一下吧

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

回答

1

你可以只分配raw_input給一個變量,並檢查變量是ballsnot 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" 
3

不知道,爲什麼你要壓縮兩個列表。

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

輸出:

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> 
+1

,所以,我要說「不知道我在做什麼」哈哈 謝謝你的迴應:> – pyjka

+0

@pyjka快樂編碼! :) –

1

有沒有需要遍歷所有的選項,你可以使用Python的優秀in關鍵字。您還需要保存的raw_input結果的地方,所以你可以比較一下:

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

你可以得到項目的動態索引:我剛學它至今

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