Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57097
| References | <33549834-2f27-47f3-abea-eb3486909dec@googlegroups.com> |
|---|---|
| Date | 2013-10-20 00:01 +1100 |
| Subject | Re: Error Testing |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1255.1382187698.18130.python-list@python.org> (permalink) |
On Sat, Oct 19, 2013 at 11:23 PM, Scott Novinger <scnovinger@gmail.com> 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)
<class 'type'>
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)
<class 'str'>
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 "<pyshell#58>", line 1, in <module>
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Error Testing Scott Novinger <scnovinger@gmail.com> - 2013-10-19 05:23 -0700
Re: Error Testing Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-19 13:37 +0100
Re: Error Testing Scott Novinger <scnovinger@gmail.com> - 2013-10-19 06:34 -0700
Re: Error Testing Chris Angelico <rosuav@gmail.com> - 2013-10-20 00:42 +1100
Re: Error Testing rusi <rustompmody@gmail.com> - 2013-10-19 09:22 -0700
Re: Error Testing Chris Angelico <rosuav@gmail.com> - 2013-10-20 09:28 +1100
Re: Error Testing Ned Deily <nad@acm.org> - 2013-10-19 15:46 -0700
Re: Error Testing Chris Angelico <rosuav@gmail.com> - 2013-10-20 10:02 +1100
Re: Error Testing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-20 12:18 -0400
Re: Error Testing Ned Batchelder <ned@nedbatchelder.com> - 2013-10-19 08:44 -0400
Re: Error Testing Roy Smith <roy@panix.com> - 2013-10-19 08:57 -0400
Re: Error Testing Chris Angelico <rosuav@gmail.com> - 2013-10-20 00:04 +1100
Re: Error Testing Ned Batchelder <ned@nedbatchelder.com> - 2013-10-19 09:07 -0400
Re: Error Testing Roy Smith <roy@panix.com> - 2013-10-19 09:09 -0400
Re: Error Testing Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-19 14:19 +0100
Re: Error Testing Chris Angelico <rosuav@gmail.com> - 2013-10-20 00:01 +1100
Re: Error Testing David Robinow <drobinow@gmail.com> - 2013-10-19 14:08 -0400
Re: Error Testing Tim Chase <tim@thechases.com> - 2013-10-19 13:31 -0500
Re: Error Testing Terry Reedy <tjreedy@udel.edu> - 2013-10-19 15:50 -0400
What's wrong with Windows Command Prompt (was Re: Error Testing) Terry Reedy <tjreedy@udel.edu> - 2013-10-19 16:35 -0400
Re: What's wrong with Windows Command Prompt (was Re: Error Testing) Chris Angelico <rosuav@gmail.com> - 2013-10-20 09:15 +1100
Re: Error Testing Chris Angelico <rosuav@gmail.com> - 2013-10-20 09:20 +1100
Re: What's wrong with Windows Command Prompt (was Re: Error Testing) David Robinow <drobinow@gmail.com> - 2013-10-21 15:55 -0400
Re: What's wrong with Windows Command Prompt (was Re: Error Testing) Tim Chase <python.list@tim.thechases.com> - 2013-10-21 15:29 -0500
csiph-web