今天我想我可能有一个简单的问题。我有一些代码要求用户从1到10中选择一个指向列表的数字。如果用户输入了错误的输入,也就是说,我将代码循环回来并要求他们做出另一个选择。到目前为止,我有以下代码,但我不确定如何使其循环。在此先感谢如何让我的代码循环,如果用户输入不正确
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:
print 'You have selected Karratha Aero as your Base Station'
elif selection == 2:
print 'You have selected Dampier Salt as your Base Station'
elif selection == 3:
print 'You have selected Karratha Station as your Base Station'
elif selection == 4:
print 'You have selected Roebourne Aero as your Base Station'
elif selection == 5:
print 'You have selected Roebourne as your Base Station'
elif selection == 6:
print 'You have selected Cossack as your Base Station'
elif selection == 7:
print 'You have selected Warambie as your Base Station'
elif selection == 8:
print 'You have selected Pyramid Station as your Base Station'
elif selection == 9:
print 'You have selected Eramurra Pool as your Base Station'
elif selection == 10:
print 'You have selected Sherlock as your Base Station'
else:
print 'You have made an error. Please chose a number from 1 to 10'
+1不使用字典时,列表就行了。 – dansalmo 2013-05-02 01:33:35
@dansalmo这只是错误的,你使用任何更具可读性,过早优化是邪恶的 – jamylak 2013-05-02 02:03:34
@jamylak,我学会了在我跑之前走路。我不考虑这种优化,我认为它使用最简单的数据结构来解决手头的问题。学习列表的基本索引功能是更复杂的关键的基础:字典的值索引。 – dansalmo 2013-05-02 15:23:16