0

今日は簡単な質問があると思います。リストを参照する 1 から 10 までの数字を選択するようにユーザーに求めるコードがあります。ユーザーが間違った入力をした場合、つまり 55 I コードをループバックし、別の選択を行うように依頼します。これまでのところ、次のコードがありますが、ループさせる方法がわかりません。前もって感謝します

    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

まず、出力する 10 個の文字列を手動で作成する代わりに、考えられるすべての基地局のリストを用意する必要があります。

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

次に、これを行うことができます:basestations[1]インデックス 1 (最初のインデックスは 0) で文字列を取得しますbasestations[selection]。これで、10 の可能性すべてに対して 1 つの print ステートメントだけが必要になります。(ヒント: を実行すると、2 つの文字列を連結できますstringa + stringb)

次に、whileループを使用します。while ループの条件は、有効な選択が行われなかった場合は true になり、有効な選択が行われた場合は false になります。とは異なりif、 a の本体はwhile最後に到達した後に戻って状態を確認し、再び true であれば再度実行します。

于 2013-05-02T01:19:32.267 に答える
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でスイッチを使用する方法

于 2013-05-02T01:24:16.923 に答える