0

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

4 回答 4

7

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

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

然后你可以这样做:basestations[1]获取索引 1 处的字符串(第一个索引为 0),例如一般basestations[selection]. 现在您只需要一份打印声明即可满足所有十种可能性。(提示:你可以通过做连接两个字符串stringa + stringb

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

于 2013-05-02T01:19:32.267 回答
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'
于 2013-05-02T01:23:08.153 回答
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'
于 2013-05-02T01:19:51.753 回答
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)

取自:如何在 python 中使用 switch

于 2013-05-02T01:24:16.923 回答