[Tutor] exceptions problem

Roelof Wobben rwobben at hotmail.com
Fri Sep 10 17:13:20 CEST 2010


Hello, 

 

I have this problem :

 

Write a function named readposint that prompts the user for a positive integer and then checks the input to confirm that it meets the requirements. A sample session might look like this:

>>> num = readposint()
Please enter a positive integer: yes
yes is not a positive integer.  Try again.
Please enter a positive integer: 3.14
3.14 is not a positive integer.  Try again.
Please enter a positive integer: -6
-6 is not a positive integer.  Try again.
Please enter a positive integer: 42
>>> num
42
>>> num2 = readposint("Now enter another one: ")
Now enter another one: 31
>>> num2
31
>>>
Now I thought this would work:def readposint(): 
    x = raw_input("Please enter a positive integer :")
    try:
        x = int(x) and x > 0 
    except:
        print x , "is not a positive integer.  Try again."
        return False
    return Truey = readposint()
print y
while y == False:
    readposint()
print "You have entered : ", yBut the x > 10 is never checked.Must I use two try except now ? Roelof    		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100910/16513e5a/attachment-0001.html>


More information about the Tutor mailing list