d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Problem
Add Reply New Topic New Poll
Member
Posts: 8,407
Joined: Jan 29 2008
Gold: 450.00
May 26 2012 02:03am
Trying to input a number between 4-9 but if not 4-9 then i want to raise an error message 'Size must be a number between 4 and 9'

Thats fine if a number is inputed but if i input words it doesnt like the int(size)

How do I change it to get it to display the error message no matter what is put in?

Code

ERROR_SIZE = "Size must be a number between 4 and 9"

size = raw_input('Please insert a vaule 4 and 9: ')
sizeint = int(size)
while sizeint > 9 or sizeint < 4:
   print ERROR_SIZE
   size = raw_input('Please insert a vaule 4 and 9: ')
   sizeint = int(size)


Thanks ;)

Jono
Member
Posts: 8,407
Joined: Jan 29 2008
Gold: 450.00
May 26 2012 02:35am
def get_board_size():
size = raw_input('Please insert a vaule 4 and 9: ')

while size.isdigit() == False or int(size) > 9 or int(size) < 4:
print ERROR_SIZE
size = raw_input('Please insert a vaule 4 and 9: ')
Member
Posts: 1,999
Joined: Sep 11 2011
Gold: 16,033.67
May 26 2012 05:10am
Code

while True:
   try:
       size = int(raw_input("Please insert a value between 4 and 9: \n"))
       if size < 4 or size > 9:
           raise Exception()
       break
   except ValueError:
       print "Oops!  That was no valid number. Try again..."
   except:
       print "Oops! That number was not between 4 and 9. Try again..."


This post was edited by feored on May 26 2012 05:16am
Member
Posts: 13,630
Joined: Dec 4 2009
Gold: 0.00
May 26 2012 08:16am
Quote (feored @ May 26 2012 04:10am)
Codewhile True:
    try:
        size = int(raw_input("Please insert a value between 4 and 9: \n"))
        if size < 4 or size > 9:
            raise Exception()
        break
    except ValueError:
        print "Oops!  That was no valid number. Try again..."
    except:
        print "Oops! That number was not between 4 and 9. Try again..."


Thank you, someone competent with Python. This guy has it.
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
May 26 2012 10:10am
A good rule of thumb (and a slightly self-evident thing that many people tend to forget) when dealing with exceptions in any language is to remember that they should be used only for exceptional conditions. People tend to try to use them for flow control and it usually makes for really bad code.
Member
Posts: 1,999
Joined: Sep 11 2011
Gold: 16,033.67
May 26 2012 11:17am
Quote (rockonkenshin @ May 26 2012 06:10pm)
A good rule of thumb (and a slightly self-evident thing that many people tend to forget) when dealing with exceptions in any language is to remember that they should be used only for exceptional conditions. People tend to try to use them for flow control and it usually makes for really bad code.


Indeed I just threw this together in a second and I'm currently working on a codebase with exceptions literally everywhere and it apparently seeped through :p
Now that I'm reading this again raising an exception if size < 4 or size > 9 is especially stupid.


Code
while True:
   size = raw_input('Please insert a value between 4 and 9: \n')
   if size.isdigit() == True and 9 > int(size) > 4:
       break
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll