Python Forum
for some reason this isnt working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for some reason this isnt working
#1
import datetime

def inputYear(msg, errmsg, l, h):
    done = 0
    while (not done):
        try:
            ans = input(msg)
            global year
            year = int(ans)
            if (year < l) or (year > h):
                print(errmsg)
            else:
                done = 1
                return year
        except:
            print(errmsg)

def inputMonth(msg, errmsg, l, h):
    done = 0
    while (not done):
        try:
            ans = input(msg)
            month = int(ans)
            if (month < l) or (month > h):
                print(errmsg)
            else:
                done = 1
                return month
        except:
            print(errmsg)

def inputDay(msg, errmsg, l, h):
    done = 0
    while (not done):
        try:
            ans = input(msg)
            day = int(ans)
            if (day < l) or (day > h):
                print(errmsg)
            elif month == (4,6,9,11):
                if day == 30:
                    print(errmsg)
            elif month == 2:
                if year % 4 >= 1 and day >= 29:
                    print(errmsg)
            else:
                done = 1
                return day
        except:
            print(errmsg)

print(inputYear('Please input year(1-9999)', 'Please input a valid year', 1, 9999))
print(inputMonth('Please input month(1-12)', 'Please input a valid month', 1, 12))
print(inputDay('Please input day(1-31)', 'Please input a valid day', 1, 31))
Reply
#2
this isnt working is not very descriptive...
It does not produce desired result? It ends with error? In case of error - post the full traceback in error tags..
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
does this
print(errmsg)
Reply
#4
What is the desired result? Some sort of date validation? Is there reason that it is done separately for year, month and day?

I observed that datetime is imported but not used.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel isnt working properly after python function is started IchNar 3 215 Yesterday, 10:27 AM
Last Post: lillydalson
  coma separator is printed on a new line for some reason tester_V 4 525 Feb-02-2024, 06:06 PM
Last Post: tester_V
  Looping if condition isnt met finndude 6 3,010 May-03-2021, 08:01 PM
Last Post: Skaperen
  Reason of the output himanshub7 2 2,197 Apr-28-2020, 10:44 AM
Last Post: TomToad
  Code used to work, not anymore for no apparent reason? chicagoxjl 1 1,911 Jan-08-2020, 05:05 PM
Last Post: jefsummers
  Syntax error for a reason I don't understand DanielCook 2 2,457 Aug-07-2019, 11:49 AM
Last Post: buran
  "invalid syntax" for no apparent reason wardancer84 2 7,190 Oct-03-2018, 11:57 AM
Last Post: wardancer84
  My program subtracts fractions, but for some reason isn't simplifying them RedSkeleton007 9 5,951 Mar-03-2018, 11:45 AM
Last Post: Gribouillis
  What is the reason for this difference between lists and tuples? Athenaeum 3 4,228 Sep-25-2017, 07:16 PM
Last Post: buran
  Why isnt this word length selector working Ivan 6 4,629 Mar-26-2017, 08:38 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020