0

This is a follow up of my other thread on 10 Green Bottles. I now want to know how i can make my input only accept some words/numbers. If the wrong word/number is written, it prompts you to type again. If the word typed match's an accepted word, it passes and runs through the rest of the code. Code:

def main():
    num1=int(input('Pick a number between 10 and 30: '))
    hue=str(input('Pick a colour; Red, Green, Blue: '))

    numbers =[ 'no', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen', 'Twenty', 'Twentyone', 'Twentytwo', 'Twentythree', 'Twentyfour', 'Twentyfive', 'Twentysix', 'Twentyseven', 'Twentyeight', 'Twentynine', 'Thirty' ]
    text_one = hue +' bottle%s\nHanging on the wall'
    text_two = "And if one " + hue + " bottle\nShould accidentally fall\nThere'll be"
    text_three =' \n'


    with open(numbers[num1] + ' ' + hue + ' Bottles.txt', 'w') as a:
        for l in range(num1, 0, -1):                                              #
            a.write(numbers[l]+ ' ')
            if l == 1:
                a.write(text_one % '' +'\n')
            else:
                a.write(text_one % 's' +'\n')

            a.write(numbers[l] + ' ')
            if l == 1:
                a.write(text_one %  '' + '\n') 
            else:
                a.write(text_one % 's' +  '\n')
            a.write(text_two + ' ')
            a.write(numbers[l-1] + ' ')
            if (l - 1) ==1 :
                a.write(text_one % ''+'\n')
            else:
                a.write(text_one % 's'+'\n')
            a.write('\n' + '\n')



if __name__ == '__main__':
    main()
Community
  • 1
  • 1
Phayer
  • 45
  • 1
  • 1
  • 9
  • Instead of describing the other question, provide a link to it. You can click the "share" button under your other question to get a link you can paste here. Without the link, that information only helps people who happen to remember your other question and know how to find it. – abarnert May 26 '15 at 23:53

1 Answers1

0

Simple while:

while True:
    try:
        msg = int(input("Enter: "))
    except ValueError:
        print("Not Valid!")
        continue
    # do some validation like if msg.endswith('hi') and stuff like that ex:

    if msg > 40 or msg < 10: # fail if greater than 40 and smaller than 10
        print("Not Valid!")
        continue
    break
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • Sorry I'm not sure how i apply this to my code, could i have a hint – Phayer May 27 '15 at 00:02
  • Use it whenever you collect input and perform validation at the same time. Otherwise, if you just want to get input until it's right, this is all you need. – Zizouz212 May 27 '15 at 00:03
  • Okay this is close to the result i want, but it only deny's strings, that's also what i want, but the main thing i want is so if the number is greater than 10 but bellow 40 it will run. If the number is not greater than 10 or greater than 40 it will fail. – Phayer May 27 '15 at 00:28
  • Then add some logic statements and validation. I'll make an edit. – Zizouz212 May 27 '15 at 00:50