0

today I think I might have an easy question. I have some code that ask the user to select a number from 1 to 10 which refers to a list. If the user makes an incorrect input ie 55 I what the code to loop back and ask them to make another selection. so far I have the following code but im unsure how to make it loop. thanks in advance

    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'
chepner
  • 497,756
  • 71
  • 530
  • 681
goat
  • 13
  • 1
  • 4

4 Answers4

7

First off, you should have a list of all possible base stations instead of manually constructing the ten strings to print, as in

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

Then you can do this: basestations[1] to get the string at index 1 (the first index is 0), e.g. in general basestations[selection]. And now you only need one print statement for all ten possibilities. (Hint: You can concatenate two strings by doing stringa + stringb)

Second, use a while loop. The condition of the while loop should be true if no valid selection was made, and false if a valid selection was made. Unlike if, the body of a while will go back and check the condition after it reaches the end, and if it's true again it will execute again.

Patashu
  • 21,443
  • 3
  • 45
  • 53
  • @dansalmo That's just wrong, you use whatever is more readable, premature optimization is evil – jamylak May 02 '13 at 02:03
  • @jamylak, I learned to walk before I ran. I do not consider this optimization, I consider it using the simplest data structure for the problem at hand. Learning the basic indexing features of a list is a foundation for the more complex key:value indexing of a dict. – dansalmo May 02 '13 at 15:23
  • @dansalmo im not talking about the best way to learn programming, im talking about the most readable solution. – jamylak May 03 '13 at 01:20
  • @jamylak, I heard you the first time. :) – dansalmo May 03 '13 at 02:20
2

One approach that you can take is to use a while-loop to ensure that the input is within a certain range.

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'
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
  • thanks, works well. I would also like it to print something like "incorrect selection " before it loops back for another input? – goat May 02 '13 at 01:49
  • Added an if-statement to check if it's the first time going through the while-loop. Personally though, I would use Patashu's solutions as well, especially for displaying the base station. – Chris Forrence May 02 '13 at 02:00
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'
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • 1
    You should explain why you made the changes you did imo, since it's a beginner python question – Patashu May 02 '13 at 01:26
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)

taken from: how to use switch in python

Community
  • 1
  • 1
oentoro
  • 740
  • 1
  • 11
  • 32