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 =---- 5 2031
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.
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 most recent results which (I
believe) should help those write their programs to be more...
|
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 in your test modules:
Choise a:
|
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
|
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
|
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
| |
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 perform
additional steps. I would like to do something like this (assume all
namespaces are set):...
|
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)))
|
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
|
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
正则表达式:
http://wiki.woodpecker.org.cn/moin/RegExpInPython#head-2358765384844ed72f01658cbcde24613d941e9d
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |