Defining a string as a series of numbers.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 8
Reputation: BlueNN is an unknown quantity at this point 
Solved Threads: 0
BlueNN BlueNN is offline Offline
Newbie Poster

Defining a string as a series of numbers.

 
0
  #1
Apr 5th, 2009
I'm trying to get my little user-input program to recognize that words aren't numbers.

Without any logic parameters the program just crashes when you put in anything but a number, I'm attempting to allow the user to try again and put in a number using a while loop, but I keep getting syntax errors when I try to define valid_age as any number between 1 and 200.

The coding for the defining of valid_age and the while loop follows.

age = 0
def valid_age (range(1, 200))
age = int(input('How old are you today? '))
while age != valid_age:
    print()
    print('That is not a valid age!')
    age = int(input('How old are you today? '))
else:
    age10 = int(age) + 10
    print()
    print('In 10 years you will be ' + str(age10) + ' years old.')
print()
Last edited by BlueNN; Apr 5th, 2009 at 10:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Defining a string as a series of numbers.

 
0
  #2
Apr 5th, 2009
Just a quick question, is this Python 3.0 or 2.6?

EDIT: nevermind, that was the stupidest question...
Last edited by shadwickman; Apr 5th, 2009 at 11:04 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,065
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 300
woooee woooee is offline Offline
Veteran Poster

Re: Defining a string as a series of numbers.

 
0
  #3
Apr 5th, 2009
Not tested but should be close. In these types of programs, you want to set a variable to assume that there are errors and unset it only when all conditions are met.
  1. errors = True
  2. while errors:
  3. age = input('How old are you today? ')
  4. try:
  5. age_int = int(age)
  6. if (age_int > 0) and (age_int < 200):
  7. errors = False
  8. print('\nIn 10 years you will be %d years old.' % (age_int+10))
  9. else:
  10. print('\nThat is not a valid age! \n')
  11. except:
  12. print('\nThat is not a valid age! \n')
Last edited by woooee; Apr 5th, 2009 at 11:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: Defining a string as a series of numbers.

 
0
  #4
Apr 6th, 2009
  1. while age not in valid_age:
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: Defining a string as a series of numbers.

 
0
  #5
Apr 6th, 2009
Also, you really should use raw_input
I suppose I could throw out a different solution:

  1. valid_age = range(1, 200)
  2. try:
  3. age = int(raw_input('How old are you today? '))
  4. except ValueError:
  5. age = 0
  6. while age not in valid_age:
  7. print('That is not a valid age!')
  8. try:
  9. age = int(raw_input('How old are you today? '))
  10. except ValueError:
  11. age = 0

Or even better:
  1. def age_input():
  2. try:
  3. return int(raw_input('How old are you today? '))
  4. except ValueError:
  5. return 0
  6.  
  7. valid_age = range(1, 200)
  8. age = age_input()
  9. while age not in valid_age:
  10. print('That is not a valid age!')
  11. age = age_input()
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Defining a string as a series of numbers.

 
0
  #6
Apr 6th, 2009
Here's my contribution:
  1. >>> def age_input(lower=1, upper=200):
  2. ... while True:
  3. ... age = raw_input("Enter an age between %d and %d" % (lower,upper))
  4. ... if age.isdigit():
  5. ... age = int(age)
  6. ... if lower <= age <= upper:
  7. ... return age
  8. ... print "Invalid answer. Try again."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,065
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 300
woooee woooee is offline Offline
Veteran Poster

Re: Defining a string as a series of numbers.

 
0
  #7
Apr 6th, 2009
Based on the statement
age = int(input('How old are you today? '))
and the print() statements, we are assuming that the OP is probably using 3.0, which doesn't have a raw_input. But someone should have asked before now. We'll have to get used to asking for the Python version number.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: BlueNN is an unknown quantity at this point 
Solved Threads: 0
BlueNN BlueNN is offline Offline
Newbie Poster

Re: Defining a string as a series of numbers.

 
0
  #8
Apr 6th, 2009
Thanks again for all of your help! I managed to get the program working and now it recognizes that words aren't valid ages.
I can't let you do that, Dave. - HAL 9000
I'm using Python 3.0.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: Defining a string as a series of numbers.

 
0
  #9
Apr 6th, 2009
You should post it for others that might come across this.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum


Views: 281 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC