我创建了一个编号为1-10的名字列表.我希望用户能够输入数字(1-10)来选择名称.我有以下代码,但仍然无法让它工作.我是python的新手.谢谢您的帮助
def taskFour():
1 == Karratha_Aero
2 == Dampier_Salt
3 == Karratha_Station
4 == Roebourne_Aero
5 == Roebourne
6 == Cossack
7 == Warambie
8 == Pyramid_Station
9 == Eramurra_Pool
10 == Sherlock
print''
print 'Choose a Base Weather Station'
print 'Enter the corresponding station number'
selection = int(raw_input('Enter a number from: 1 to 10'))
if selection == 1:
selectionOne()
elif selection == 2:
selectionTwo()
elif selection == 3:
selectionThree()
Run Code Online (Sandbox Code Playgroud)
你正在遵循反模式.当有一百万个不同的电台或每个电台有多个数据时,您打算做什么?
你可以不必selectionOne()通过一路selectionOneMillion()手动完成.
这样的事情怎么样:
stations = {'1': "Karratha_Aero",
'2': "Karratha_Station",
'10': "Sherlock"}
user_selection = raw_input("Choose number: ")
print stations.get(user_selection) or "No such station"
Run Code Online (Sandbox Code Playgroud)
输入输出:
1 => Karratha_Aero
10 => Sherlock
5 => No such station
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |