I’m trying to write a script that accepts input for a number, and then checks to see
(a) that the input is in fact a number, and
(b) that the number in question is less than or equal to 17.
I’ve tried a variety of “if” statements to no avail, and now I’m trying to wrap my head around “try” statements. This is my best attempt to date:
def listlength():
print "How many things (up to 17) do you want in the list?"
global listlong
listlong = raw_input("> ")
try:
listlong = int(listlong)
listlong <= 17
except:
print "Gotta be a number less than 17, chumpy!"
listlength()
liststretcher()
It works for the first element in the try: if it’s not a numeral, I have to run through the listlength function again. But the second element (<=17) is completely ignored.
I’ve also tried
try:
listlong = int(listlong) and listlong <= 17
…but that still gives me only a functional first check, and ignores the second entirely.
I also get the same result if I have two try statements:
try:
listlong = int(listlong)
except:
print "Gotta be a number, chumpy!"
listlength()
try:
listlong <=17
except:
print "Gotta be less than 17!"
listlength()
liststretcher()
Is there a way to have try: check two things, and require both to pass before moving past the exception? Or do I have to make two different try: statements in the same definition before moving on to the liststretcher() command?
In response to S.Lott, below: my intention was that “try: listlong <=17” would check to see if the “listlong” variable was shorter than or equal to 17; if that check failed, it would then move to the “except”; if it passed, it would move on to liststretcher() below.
Reading the answers to date, I’ve got about eight things to follow up on…
You have most of the answer: