•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,529 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,754 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: 1501 | Replies: 11
![]() |
You don't have to exit the program, just stay in a loop until conditions are met ...
Things get hairy with correct days.
If you want to exit a program, this line of code will do ...
python Syntax (Toggle Plain Text)
while True: date = raw_input("Enter a date in the form YYYY/MM/DD:") # slice the string and convert to integer values year = int(date[0:4]) month = int(date[5:7]) day = int(date[8:10]) if month in range(1, 13): # ok, break out of the while loop break else: # stay in the while loop print "month should be between 01 and 12" # test print year, month, day
If you want to exit a program, this line of code will do ...
raise SystemExit
Last edited by vegaseat : Oct 9th, 2007 at 1:35 am.
May 'the Google' be with you!
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
Validating input is one of my big themes with my students. It's usually accomplished like this:
That's the usual framework, then you can fill in the details.
Once you've learned functions, you can throw all the validation into a function.
Hope it helps,
Jeff
Python Syntax (Toggle Plain Text)
(note no 'inlinecode' tags) while True: inp = raw_input("prompt: ") if inp_is_valid: process_inp break else: print "Helpful Error Message" ...
That's the usual framework, then you can fill in the details.
Once you've learned functions, you can throw all the validation into a function.
Hope it helps,
Jeff
Last edited by jrcagle : Oct 9th, 2007 at 9:36 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Help with Creating a Yearly Calendar (C++)
- validate days in a month (Pascal and Delphi)
- Pop-Up Calendar for ASP (ASP)
- calendar problem again (C)
- calendar problem (C)
- calendar how mny days (C)
- how many days in each month? (Software Developers' Lounge)
Other Threads in the Python Forum
- Previous Thread: Ping in a loop
- Next Thread: initalze class by a condition



Linear Mode