473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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('\nType 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 2021

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.

----== 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.findall(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('\nType a number from 1 to 20')
if TEST :
Do_A
else:
Do_B
---
Thanks for any guidance.
x=raw_input('\nType 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('\nType 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('\nType 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('\nType 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
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...
4
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...
0
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: *** ...
6
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...
3
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...
3
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...
38
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
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...
2
by: Netkiller | last post by:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Project: Network News Transport Protocol Server Program Description: 基于数据库的新闻组,实现BBS前端使用NNTP协议来访问贴子...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.