•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 427,223 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,283 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 806 | Replies: 16 | Solved
![]() |
There are a few possibilities I can think of, but probably the most straight forward:
Now when you say restrict this to positive numbers, do you mean:
If Input is not <a positive number>:
perform action
python Syntax (Toggle Plain Text)
>>> li = [ 1, 2.3, 4e-5, 'hey!' ] >>> for item in li: ... test = str(item) ... if test == item: ... print item, 'String!' ... else: ... print item, 'Not String!' ... 1 Not String! 2.3 Not String! 4e-005 Not String! hey! String! >>>
Now when you say restrict this to positive numbers, do you mean:
If Input is not <a positive number>:
perform action
Let's Go Pens!
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
•
•
•
•
yep exactly, if they input a negative number it will return differentiate that form a positive number.
Also thanks alot for what you have given me.
python Syntax (Toggle Plain Text)
>>> li = [ 1, -2, 2.3, 't', -5.3e5, -0.1, 4e-5, 'hey!' ] >>> for item in li: ... test = str(item) ... if test == item: ... print item, 'String!' ... else: ... if item < 0: ... print item, 'Not string... negative!' ... else: ... print item, 'Not string... positive!' ... 1 Not string... positive! -2 Not string... negative! 2.3 Not string... positive! t String! -530000.0 Not string... negative! -0.1 Not string... negative! 4e-005 Not string... positive! hey! String! >>>
Just remember to do this only after you've verified that the item is not a string or else you will run into some strange results.
Let's Go Pens!
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation:
Rep Power: 10
Solved Threads: 176
This is just jlm699's code wrapped into a function ...
python Syntax (Toggle Plain Text)
def isPositiveNumber(n): """returns True if n is a positive number, else returns False""" # check if n is a string q = str(n) if q == n: return False if n > 0: return True else: return False # test the function if __name__ == '__main__': mylist = li = [ 1, -2, 2.3, 't', -5.3e5, -0.1, 4e-5, 'hey!' ] for item in mylist: print item, isPositiveNumber(item)
May 'the Google' be with you!
•
•
Join Date: Dec 2006
Posts: 450
Reputation:
Rep Power: 2
Solved Threads: 62
If you are using raw_input or the variable is already a string, then try isdigit(). This of course assumes that you aren't using floating point numbers. If you are then you will have to use a try/except converting the string to a float.
get_num=raw_input("Enter a number ")
print get_num,
if get_num.isdigit():
if int(get_num) > 0:
print "is integer and is positive"
else:
print "is integer and is NOT positive"
## this will never execute since a minus sign, "-", is not a digit
else:
print "is NOT a number"•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
Or this:
If you want to allow floating point numbers, you can change the int() to float()
Jeff
Python Syntax (Toggle Plain Text)
try: val = int(item) if val > 0: do stuff for positive numbers elif val < 0: do stuff for negative numbers else: # don't omit zero! do stuff for zero except: pass
If you want to allow floating point numbers, you can change the int() to float()
Jeff
•
•
Join Date: Jan 2008
Posts: 10
Reputation:
Rep Power: 1
Solved Threads: 0
python Syntax (Toggle Plain Text)
def valid(x): # check if n is a string q = str(x) if q == x: print "Error, Not valid input" break else: if x > 0: print "" else: print "Error, Not valid input" break
I am using this to break a loop if there is invalid input.
Thank you for all your help, it solved quite a big problem for me
Last edited by linkrulesx10 : Jul 14th, 2008 at 5:14 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Random number generator's (C++)
- Recursive prime number f(x) (C)
- Control number of IE windows that open. (Web Browsers)
- Calculate the factorial of a number (Visual Basic 4 / 5 / 6)
- Help with random number gen (C++)
- Finding out Google PR number (Search Engine Optimization)
Other Threads in the Python Forum
- Previous Thread: receive bytes streams
- Next Thread: Help! Finding a point on a line program



Linear Mode