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 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
Reply
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: noob in need of guidance for calendar (days of month)

  #11  
Oct 9th, 2007
You don't have to exit the program, just stay in a loop until conditions are met ...
  1. while True:
  2. date = raw_input("Enter a date in the form YYYY/MM/DD:")
  3. # slice the string and convert to integer values
  4. year = int(date[0:4])
  5. month = int(date[5:7])
  6. day = int(date[8:10])
  7. if month in range(1, 13):
  8. # ok, break out of the while loop
  9. break
  10. else:
  11. # stay in the while loop
  12. print "month should be between 01 and 12"
  13.  
  14. # test
  15. print year, month, day
Things get hairy with correct days.

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!
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: noob in need of guidance for calendar (days of month)

  #12  
Oct 9th, 2007
Validating input is one of my big themes with my students. It's usually accomplished like this:

  1. (note no 'inlinecode' tags)
  2.  
  3. while True:
  4. inp = raw_input("prompt: ")
  5. if inp_is_valid:
  6. process_inp
  7. break
  8. else:
  9. print "Helpful Error Message"
  10. ...

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.
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 4:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC