如果用户输入不正确,我如何使我的代码循环

时间:2013-05-02 01:16:31

标签: python loops if-statement

今天我想我可能会有一个简单的问题。我有一些代码要求用户选择1到10之间的数字,这是指列表。如果用户输入的输入不正确,即55我要循环的代码,并要求他们进行另一个选择。到目前为止,我有以下代码,但我不确定如何使其循环。提前谢谢

    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'

4 个答案:

答案 0 :(得分:7)

首先,您应该拥有所有可能基站的列表,而不是手动构建要打印的十个字符串,如

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

然后你可以这样做:basestations[1]得到索引1处的字符串(第一个索引是0),例如一般来说basestations[selection]。现在,您只需要一个打印语句来表示所有十种可能性。 (提示:您可以通过执行stringa + stringb

来连接两个字符串

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

答案 1 :(得分: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'

答案 2 :(得分: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'

答案 3 :(得分: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