Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'interpreter': 0.05; 'string.': 0.05; 'subject:Error': 0.07; 'literal': 0.09; 'type,': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'windows': 0.15; 'failure.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer,': 0.16; 'integer.': 0.16; 'string:': 0.16; '(you': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'entered': 0.20; 'work,': 0.20; 'help.': 0.21; '>>>': 0.22; 'error': 0.23; 'integer': 0.24; 'header:In-Reply- To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; '"",': 0.31; 'block,': 0.31; 'file': 0.32; 'there.': 0.32; 'probably': 0.32; '(most': 0.33; 'checking': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'idle': 0.36; 'thanks': 0.36; 'turn': 0.37; 'easily': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'conversion': 0.61; "you're": 0.61; 'circle': 0.68; 'invalid': 0.68; 'line,': 0.68; 'default': 0.69; 'press': 0.70; '10:': 0.84; 'effectively,': 0.84; 'fails,': 0.84; 'subject:Testing': 0.84; 'succeed.': 0.84; 'want:': 0.84; 'scott': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=A2FdGLVDHeLN/tjYFixTLmg5/pb4OCDyIfetx7FYjZ0=; b=sBrlMoHzaTG1Hptr5qin+G8t3STfGRi8pFUDcgREx6/6gxR0DgvXj9F7cNm2r3KIoQ T9hBtK7/wozBEQ802+kB61J0VI/UaiCgh5GGmbmL+AEFHS0AaxIJ6mOYNLaE/i1l/0kD cdU3xvz2Bbav4CiN6uZ8pYTGoFQAKfmIydSEL1aI1reC6kI4PMh9IfH23SVI0mX+bl+N WI4a3bp8N+aWW6o89zmwc4YULWHjmAqJK0Aw2oVMxFSZRmz7AVT9p3Q0BwHpgDHb/t2U 4XOXLsAis8561KFd6qaacK78b4s6YaQfCAcsFk6NX7Gj6pyAMoYmYCGrDMh1s4P6PIcV pDyw== MIME-Version: 1.0 X-Received: by 10.68.48.166 with SMTP id m6mr7942247pbn.105.1382187688844; Sat, 19 Oct 2013 06:01:28 -0700 (PDT) In-Reply-To: <33549834-2f27-47f3-abea-eb3486909dec@googlegroups.com> References: <33549834-2f27-47f3-abea-eb3486909dec@googlegroups.com> Date: Sun, 20 Oct 2013 00:01:28 +1100 Subject: Re: Error Testing From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382187698 news.xs4all.nl 15878 [2001:888:2000:d::a6]:49281 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57097 On Sat, Oct 19, 2013 at 11:23 PM, Scott Novinger wrote: > # Create the variable for radius, "radius". > print('Please enter the circle radius and press ENTER:') > radius = input() > > # Check to make sure the entered value is an integer. > if type(radius) != type(int): > print('You must enter an integer value.') > print('Please enter the circle radius and press ENTER:') > radius = input() > else: > print('The radius you entered is: ' + radius) > > radius = int(radius) > > Thanks for your help. I'm using Python v3.2 for windows. In Python 3, the input() function always returns a string. Your type check is never going to succeed. Also, you're not checking what you think you're checking; you're actually looking to see if input() returned a type, because the type of int is type: >>> type(int) You might want: if type(radius) != int: But that still won't work, because input() will always return a string: >>> radius = input() 42 >>> type(radius) You can try all these out in the interactive interpreter (you probably have IDLE installed, which on Windows is rather nicer to work with than the default interactive mode). A better check would be to just try to turn the inputted value into an integer, and fail if that doesn't work. Again, try this interactively: >>> int("42") 42 >>> int("not an integer") Traceback (most recent call last): File "", line 1, in int("not an integer") ValueError: invalid literal for int() with base 10: 'not an integer' With a couple of quick checks, you can easily see what happens if the conversion fails, and then you can deal with that failure. So effectively, just drop the whole if: block, go straight to the "radius = int(radius)" line, and deal with the error there. ChrisA