| | |
Defining a string as a series of numbers.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
Just a quick question, is this Python 3.0 or 2.6?
EDIT: nevermind, that was the stupidest question...
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
- Hunter S. Thompson
my photography
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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)
errors = True while errors: age = input('How old are you today? ') try: age_int = int(age) if (age_int > 0) and (age_int < 200): errors = False print('\nIn 10 years you will be %d years old.' % (age_int+10)) else: print('\nThat is not a valid age! \n') except: print('\nThat is not a valid age! \n')
Last edited by woooee; Apr 5th, 2009 at 11:47 pm.
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
python Syntax (Toggle Plain Text)
while age not in valid_age:
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
Also, you really should use
I suppose I could throw out a different solution:
Or even better:
raw_input I suppose I could throw out a different solution:
python Syntax (Toggle Plain Text)
valid_age = range(1, 200) try: age = int(raw_input('How old are you today? ')) except ValueError: age = 0 while age not in valid_age: print('That is not a valid age!') try: age = int(raw_input('How old are you today? ')) except ValueError: age = 0
Or even better:
python Syntax (Toggle Plain Text)
def age_input(): try: return int(raw_input('How old are you today? ')) except ValueError: return 0 valid_age = range(1, 200) age = age_input() while age not in valid_age: print('That is not a valid age!') age = age_input()
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Here's my contribution:
Python Syntax (Toggle Plain Text)
>>> def age_input(lower=1, upper=200): ... while True: ... age = raw_input("Enter an age between %d and %d" % (lower,upper)) ... if age.isdigit(): ... age = int(age) ... if lower <= age <= upper: ... return age ... print "Invalid answer. Try again."
![]() |
Other Threads in the Python Forum
- Previous Thread: Taking a variable out of a Tkinter GUI
- Next Thread: Pickle Module Error
Views: 285 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython






