goat
goat

Reputation: 13

how do i make my code loop if the users input is incorrect

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'

Upvotes: 0

Views: 9751

Answers (4)

Chris Forrence
Chris Forrence

Reputation: 10094

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'

Upvotes: 2

oentoro
oentoro

Reputation: 750

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

Upvotes: 0

jamylak
jamylak

Reputation: 133564

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'

Upvotes: 2

Patashu
Patashu

Reputation: 21773

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.

Upvotes: 7

Related Questions