Python 如何根据所提供的列表进行原始输入选择?选择时,请给出正确的结果?
我想要的是,基于原始输入问题和提供的列表 我想要一个与从列表中选择的项目(如足球、橄榄球、篮球等)相关的打印结果 如果有人能帮我,我将不胜感激Python 如何根据所提供的列表进行原始输入选择?选择时,请给出正确的结果?,python,list,raw-input,Python,List,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 = ['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" `
您只需将
raw_输入
分配给一个变量,然后检查变量是否在balls
或不在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"
您只需将
raw_输入
分配给一个变量,然后检查变量是否在balls
或不在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"
不知道,为什么要压缩两个列表
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>
不知道,为什么要压缩两个列表
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>
不需要对选项进行循环,您可以在关键字中使用python优秀的
。您还需要将原始输入的结果保存在某个地方,以便进行比较:
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"
不需要对选项进行循环,您可以在
关键字中使用python优秀的。您还需要将原始输入的结果保存在某个地方,以便进行比较:
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"
您可以动态获取项目的索引:
choice = raw_input("Please chose a sport you want to play: ")
if choice in balls:
print (balls[balls.index(choice)])
您可以动态获取项目的索引:
choice = raw_input("Please chose a sport you want to play: ")
if choice in balls:
print (balls[balls.index(choice)])
我只是学习到目前为止,所以我会说“不知道我在做什么”哈哈谢谢你的回复:>我只是学习到目前为止,所以我会说“不知道我在做什么”哈哈谢谢你的回复:>