473,543 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Test for number?

In the following code I would like to ascertain
that x has/is a number. What the simplest TEST should be?
(Could not find good example yet.)
---
x=raw_input('\n Type a number from 1 to 20')
if TEST :
Do_A
else:
Do_B
---
Thanks for any guidance.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Sep 4 '06 #1
5 2024

Dr. Pastor wrote:
In the following code I would like to ascertain
that x has/is a number. What the simplest TEST should be?
(Could not find good example yet.)
---
x=raw_input('\n Type a number from 1 to 20')
if TEST :
Do_A
else:
Do_B
---
Thanks for any guidance.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
To test if it *is* a number, the solution is pretty simple (s.isdigit()
or an int(s) in a try..except ValueError), but if you want to test if
it is or *has* a number, I think that the simplest solution would be a
regexp:

import re
re_has_digit = re.compile(r'\d +')
try:
digits = re_has_digit.fi ndall(input)[0]
number = int(digits)
print "%d is a number." % number
except IndexError:
print "'%s' has no number in it." % input

I didn't try it, but it should work.

Sep 4 '06 #2
Dr. Pastor wrote:
In the following code I would like to ascertain
that x has/is a number. What the simplest TEST should be?
(Could not find good example yet.)
---
x=raw_input('\n Type a number from 1 to 20')
if TEST :
Do_A
else:
Do_B
---
Thanks for any guidance.
x=raw_input('\n Type a number from 1 to 20')
try:
x = int(x)
if x<1 or x>20: raise ValueError()
except ValueError:
Do_B
else:
Do_A
If you want to distinguish between the two error cases (not a number vs
number not in [1,20]), handle the second one as "Do_C" instead of
raising ValueError.

HTH,
George

Sep 4 '06 #3
On 2006-09-04, George Sakkis <ge***********@ gmail.comwrote:
Dr. Pastor wrote:
>In the following code I would like to ascertain that x has/is
a number. What the simplest TEST should be? (Could not find
good example yet.)
---
x=raw_input('\ nType a number from 1 to 20')
if TEST :
Do_A
else:
Do_B
---
Thanks for any guidance.

x=raw_input('\n Type a number from 1 to 20')
try:
x = int(x)
if x<1 or x>20: raise ValueError()
except ValueError:
Do_B
else:
Do_A

If you want to distinguish between the two error cases (not a
number vs number not in [1,20]), handle the second one as
"Do_C" instead of raising ValueError.
Is the original value of x available in Do_B and Do_A, or will it
have been clobbered before getting there?

--
Neil Cerutti
Sep 5 '06 #4
Neil Cerutti wrote:
On 2006-09-04, George Sakkis <ge***********@ gmail.comwrote:
x=raw_input('\n Type a number from 1 to 20')
try:
x = int(x)
if x<1 or x>20: raise ValueError()
except ValueError:
Do_B
else:
Do_A

If you want to distinguish between the two error cases (not a
number vs number not in [1,20]), handle the second one as
"Do_C" instead of raising ValueError.

Is the original value of x available in Do_B and Do_A, or will it
have been clobbered before getting there?
In Do_A, x will be an integer between 1 and 20. In Do_B, it depends; if
the original input cannot be converted to an int, it will be preserved,
otherwise x will be an integer (lower than 1 or larger than 20).

George

Sep 5 '06 #5
On 2006-09-05, George Sakkis <ge***********@ gmail.comwrote:
Neil Cerutti wrote:
>On 2006-09-04, George Sakkis <ge***********@ gmail.comwrote:
x=raw_input('\n Type a number from 1 to 20')
try:
x = int(x)
if x<1 or x>20: raise ValueError()
except ValueError:
Do_B
else:
Do_A

If you want to distinguish between the two error cases (not a
number vs number not in [1,20]), handle the second one as
"Do_C" instead of raising ValueError.

Is the original value of x available in Do_B and Do_A, or will
it have been clobbered before getting there?

In Do_A, x will be an integer between 1 and 20. In Do_B, it
depends; if the original input cannot be converted to an int,
it will be preserved, otherwise x will be an integer (lower
than 1 or larger than 20).
Thanks. I infer from this that in the case that int(x) raises
ValueError, that x is guaranteed to be unmodified.

--
Neil Cerutti
22 members were present at the church meeting held at the home
of Mrs. Marsha Crutchfield last evening. Mrs. Crutchfield and
Mrs. Rankin sang a duet, The Lord Knows Why. --Church Bulletin Blooper
Sep 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4287
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of code everytime I found more than one method to perform a single task. I timed each method to find which would complete faster. I thought I'd share my...
4
3052
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and huge test classes, I started to think about splitting classes. Basically you have at least three obvious choises, if you are going for consistency...
0
2282
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** Invalid arguments to divll. at test_eng/Testout.pm line 30. ...propagated at t/polyser.t line 9. t/polyser.....dubious
6
23432
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really three and they validate a four items
3
1740
by: Rick | last post by:
Hello, I ran Microsoft's free "Web Application Stress" tool to see how asp.net/c# performed against html. Are these results typical? Network: WAS ran on a server with a t3 Internet connection to the test server. Test Server: Windows 2000 SP 4
3
14269
by: tldisbro | last post by:
Hello All, I am trying to use the returned value of the <fo:page-number> element/function in my <xsl:if> test condition. But am unsuccessful in doing so. Is it possible to use it in this fashion with a conversion or correct syntax? I would like to test the current page number and see if it is even or odd - and if it is odd I would like to...
38
4502
by: Astra | last post by:
Hi All Could somebody please confirm that if I change my JS expression test from: if (!(/^*$/.test(document.form1.fred.value))) to if (!(/^*$/.test(document.form1.fred.value)))
5
2381
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix it. It reads in the file and sorts out the files into the four different lists. F.txt int main 2 " " help
2
2408
by: Netkiller | last post by:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Project: Network News Transport Protocol Server Program Description: 基于数据库的新闻组,实现BBS前端使用NNTP协议来访问贴子 Reference: NNTP协议: http://www.mibsoftware.com/userkt/0099.htm 正则表达式:...
0
7355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1830
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
979
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.