Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57094
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <nedbat@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'correct.': 0.07; 'subject:Error': 0.07; 'string': 0.09; '"class"': 0.09; 'arc': 0.09; 'converted': 0.09; 'rewrite': 0.09; 'try:': 0.09; 'type,': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'integer.': 0.16; 'it),': 0.16; 'length.': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'properly': 0.19; 'value.': 0.19; 'entered': 0.20; 'written': 0.21; 'help.': 0.21; 'header:User-Agent:1': 0.23; 'error': 0.23; 'integer': 0.24; 'string,': 0.24; '(or': 0.24; "i've": 0.25; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'testing': 0.29; 'am,': 0.29; "doesn't": 0.30; 'converting': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'second,': 0.31; 'values.': 0.31; 'could': 0.34; 'except': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'thanks': 0.36; 'to:addr:python- list': 0.38; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'to:addr:gmail.com': 0.65; 'circle': 0.68; 'press': 0.70; 'goal': 0.75; 'subject:Testing': 0.84; 'want:': 0.84; 'scott': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=sPOu117xSvyG0N0favtH8JUF5XqLBEO8GsiT35b9scU=; b=vW4xHt/QOSlU9kViUPCHSDx++7yjQKwGor+tWlDJJ5K1aF3nc9UiXDieVzh50OSPzF e2wQh9LOyxZ2dOGjI39htZ7jol6iC14F+/UPi0m+I6u5mxffpObWJyev3e4cXb8isvgW 0M+51KJJPdsncswIFksCa44A1eE3voDZFjTZ5qGT04nSf7y7rjO91kWpctoR6tt4GkDo GZ5aPiGB3j0uivB5h9u8wpM2KtHzoOFNT/8KKJtpFAFU4rvCDGh2k6Iev6vGny7A6anQ 4c7gH9+ejykGNyZttYenWrtypuJD16FpJyb9toBIDAO/MOjWl1C7uXkf8aTyyOdL637H 5Mqg== |
| X-Received | by 10.49.131.200 with SMTP id oo8mr10843975qeb.0.1382186682701; Sat, 19 Oct 2013 05:44:42 -0700 (PDT) |
| Sender | Ned Batchelder <nedbat@gmail.com> |
| Date | Sat, 19 Oct 2013 08:44:42 -0400 |
| From | Ned Batchelder <ned@nedbatchelder.com> |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.0.1 |
| MIME-Version | 1.0 |
| To | Scott Novinger <scnovinger@gmail.com>, python-list@python.org |
| Subject | Re: Error Testing |
| References | <33549834-2f27-47f3-abea-eb3486909dec@googlegroups.com> |
| In-Reply-To | <33549834-2f27-47f3-abea-eb3486909dec@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1254.1382186691.18130.python-list@python.org> (permalink) |
| Lines | 46 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1382186691 news.xs4all.nl 15957 [2001:888:2000:d::a6]:36785 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:57094 |
Show key headers only | View raw
On 10/19/13 8:23 AM, Scott Novinger wrote:
> Hello.
>
> I've written a program for my kids to calculate arc length. I want to include some error testing for value types entered that are something other than integer values.
>
> My goal is to make sure that the value entered for the radius is an integer value.
>
> How could I rewrite this code to make sure I accomplish my goal of getting an integer value entered? I know the construct is not correct. I'm just learning how to program.
>
> # 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.
>
> Scott
Hi Scott, welcome!
This line doesn't do what you want:
if type(radius) != type(int):
for a few reasons: First, radius is the result of input(), so it is
always a string, never an int. Second, "int" itself is already a type,
so type(int) is actually "class" (or something like it), not "int".
What you want is to know whether the string inputted can be properly
converted to an int. In Python, the way to do that is to try converting
it, and be prepared for it to fail:
try:
radius = int(radius)
except ValueError:
print('You must enter an integer value.')
--Ned.
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