2013-05-02 24 views
0

今天我想我可能有一个简单的问题。我有一些代码要求用户从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' 

回答

7

首先,你应该有所有可能的基站的列表,而不是手动构建十根弦打印,如

basestations = ["", "Karratha Aero", "Dampier Salt", ...]

然后,你可以这样做: basestations[1]获得索引为1的字符串(第一个索引为0),例如一般basestations[selection]。现在你只需要一个打印语句就可以完成所有的十种可能性。 (提示:可以通过做stringa + stringb连接两个字符串)

二,使用一个while循环。如果没有有效的选择,while循环的条件应该为真,如果进行了有效的选择,则为false。与if不同,while的主体将返回,并在达到结尾后检查条件,如果它再次为真,则它将再次执行。

+0

+1不使用字典时,列表就行了。 – dansalmo 2013-05-02 01:33:35

+0

@dansalmo这只是错误的,你使用任何更具可读性,过早优化是邪恶的 – jamylak 2013-05-02 02:03:34

+0

@jamylak,我学会了在我跑之前走路。我不考虑这种优化,我认为它使用最简单的数据结构来解决手头的问题。学习列表的基本索引功能是更复杂的关键的基础:字典的值索引。 – dansalmo 2013-05-02 15:23:16

2

您可以采取的一种方法是使用while循环来确保输入在一定范围内。

selection = 0 
first = True 
print 'Choose a Base Weather Station' 
print 'Enter the corresponding station number' 
while selection < 1 or selection > 10: 
    if(first == True): 
     first = False 
    else: 
     print 'You have made an error. Please choose a number from 1 to 10' 

    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 'Something went wrong' 
+0

谢谢,效果很好。我还希望它在循环返回另一个输入之前打印“不正确选择”之类的内容? – goat 2013-05-02 01:49:45

+0

添加了一个if语句来检查它是否是第一次经历while循环。就个人而言,我也会使用Patashu的解决方案,特别是用于显示基站。 – 2013-05-02 02:00:17

2
base_stations = {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 'Choose a Base Weather Station' 
print 'Enter the corresponding station number' 
while True: 
    selection = int(raw_input('Enter a number from: 1 to 10')) 
    if selection in base_stations: 
     print('You have selected {station} as your base station'.format(
       station=base_stations[selection])) 
     break 
    else: 
     print 'You have made an error. Please chose a number from 1 to 10' 
+1

你应该解释你为什么做了你所做的改变,因为它是一个初学者python问题 – Patashu 2013-05-02 01:26:31

0
def f(x): 
    return { 
     1 : 'You have selected Karratha Aero as your Base Station', 
     ... 
    }[x] 

selection = 0 
print 'Choose a Base Weather Station' 
print 'Enter the corresponding station number' 
while selection < 1 or selection > 10: 
     selection = int(raw_input('Enter a number from: 1 to 10')) 
     f(selection) 

摘自:how to use switch in python

相关问题