User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jan 2008
Posts: 10
Reputation: linkrulesx10 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
linkrulesx10 linkrulesx10 is offline Offline
Newbie Poster

If <not number>?

  #1  
Jul 11th, 2008
I need an if statement that will run if the value is not a number.
I am unsure how to do this in python I have tried looking around and I can't seem to find what I need.

If Input is not <a number>

Also if you can help me restrict this to positive numbers that would be appreciated as well.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: If <not number>?

  #2  
Jul 11th, 2008
There are a few possibilities I can think of, but probably the most straight forward:
  1. >>> li = [ 1, 2.3, 4e-5, 'hey!' ]
  2. >>> for item in li:
  3. ... test = str(item)
  4. ... if test == item:
  5. ... print item, 'String!'
  6. ... else:
  7. ... print item, 'Not String!'
  8. ...
  9. 1 Not String!
  10. 2.3 Not String!
  11. 4e-005 Not String!
  12. hey! String!
  13. >>>

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. **
Reply With Quote  
Join Date: Jan 2008
Posts: 10
Reputation: linkrulesx10 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
linkrulesx10 linkrulesx10 is offline Offline
Newbie Poster

Re: If <not number>?

  #3  
Jul 11th, 2008
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.
Last edited by linkrulesx10 : Jul 11th, 2008 at 11:49 am.
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 206
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 31
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz in Training

Re: If <not number>?

  #4  
Jul 11th, 2008
Originally Posted by linkrulesx10 View Post
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.


  1. >>> li = [ 1, -2, 2.3, 't', -5.3e5, -0.1, 4e-5, 'hey!' ]
  2. >>> for item in li:
  3. ... test = str(item)
  4. ... if test == item:
  5. ... print item, 'String!'
  6. ... else:
  7. ... if item < 0:
  8. ... print item, 'Not string... negative!'
  9. ... else:
  10. ... print item, 'Not string... positive!'
  11. ...
  12. 1 Not string... positive!
  13. -2 Not string... negative!
  14. 2.3 Not string... positive!
  15. t String!
  16. -530000.0 Not string... negative!
  17. -0.1 Not string... negative!
  18. 4e-005 Not string... positive!
  19. hey! String!
  20. >>>
Simply check if the number is less than zero to determine if it is negative.
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. **
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: If <not number>?

  #5  
Jul 12th, 2008
This is just jlm699's code wrapped into a function ...
  1. def isPositiveNumber(n):
  2. """returns True if n is a positive number, else returns False"""
  3. # check if n is a string
  4. q = str(n)
  5. if q == n:
  6. return False
  7. if n > 0:
  8. return True
  9. else:
  10. return False
  11.  
  12. # test the function
  13. if __name__ == '__main__':
  14. mylist = li = [ 1, -2, 2.3, 't', -5.3e5, -0.1, 4e-5, 'hey!' ]
  15. for item in mylist:
  16. print item, isPositiveNumber(item)
May 'the Google' be with you!
Reply With Quote  
Join Date: Dec 2006
Posts: 450
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 62
woooee woooee is offline Offline
Posting Pro in Training

Re: If <not number>?

  #6  
Jul 12th, 2008
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"
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: If <not number>?

  #7  
Jul 12th, 2008
Or this:

  1. try:
  2. val = int(item)
  3. if val > 0:
  4. do stuff for positive numbers
  5. elif val < 0:
  6. do stuff for negative numbers
  7. else: # don't omit zero!
  8. do stuff for zero
  9. except:
  10. pass

If you want to allow floating point numbers, you can change the int() to float()

Jeff
Reply With Quote  
Join Date: Jan 2008
Posts: 10
Reputation: linkrulesx10 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
linkrulesx10 linkrulesx10 is offline Offline
Newbie Poster

Re: If <not number>?

  #8  
Jul 14th, 2008
  1. def valid(x):
  2. # check if n is a string
  3. q = str(x)
  4. if q == x:
  5. print "Error, Not valid input"
  6. break
  7. else:
  8.  
  9. if x > 0:
  10. print ""
  11. else:
  12. print "Error, Not valid input"
  13. 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.
Reply With Quote  
Join Date: Jan 2008
Posts: 10
Reputation: linkrulesx10 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
linkrulesx10 linkrulesx10 is offline Offline
Newbie Poster

Re: If <not number>?

  #9  
Jul 14th, 2008
Ok, Modules Idea is not working as it sets that as something different outside of the loop it is currently in so any variables (and break commands) seem to not work...
Is there a way I can make a variable global or somehow allow a module to break a loop.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: If <not number>?

  #10  
Jul 14th, 2008
Instead of the break have your function return False and respond to that in your loop.
Last edited by vegaseat : Jul 14th, 2008 at 8:05 am.
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 11:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC