943,514 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 505
  • Python RSS
Apr 5th, 2009
0

Defining a string as a series of numbers.

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlueNN is offline Offline
8 posts
since Apr 2009
Apr 5th, 2009
0

Re: Defining a string as a series of numbers.

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.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Apr 5th, 2009
0

Re: Defining a string as a series of numbers.

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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,301 posts
since Dec 2006
Apr 6th, 2009
0

Re: Defining a string as a series of numbers.

python Syntax (Toggle Plain Text)
  1. while age not in valid_age:
Reputation Points: 22
Solved Threads: 28
Junior Poster
adam1122 is offline Offline
181 posts
since Mar 2009
Apr 6th, 2009
0

Re: Defining a string as a series of numbers.

Also, you really should use raw_input
I suppose I could throw out a different solution:

python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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()
Reputation Points: 22
Solved Threads: 28
Junior Poster
adam1122 is offline Offline
181 posts
since Mar 2009
Apr 6th, 2009
0

Re: Defining a string as a series of numbers.

Here's my contribution:
Python Syntax (Toggle Plain Text)
  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."
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Apr 6th, 2009
0

Re: Defining a string as a series of numbers.

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.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,301 posts
since Dec 2006
Apr 6th, 2009
0

Re: Defining a string as a series of numbers.

Thanks again for all of your help! I managed to get the program working and now it recognizes that words aren't valid ages.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlueNN is offline Offline
8 posts
since Apr 2009
Apr 6th, 2009
0

Re: Defining a string as a series of numbers.

You should post it for others that might come across this.
Reputation Points: 22
Solved Threads: 28
Junior Poster
adam1122 is offline Offline
181 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Taking a variable out of a Tkinter GUI
Next Thread in Python Forum Timeline: Pickle Module Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC