0

When I get it right the first time I receive 'mwindow'. However, if I get it wrong once or more times I always get 'none' even if I get it right in the end.

def windows():
    print('\n---Maintenence Window Config---\n')
    mwdate = input('Enter the date for the maintenance window (3 letters, Mon, Tue, Wed, etc.) : ')
    if len(mwdate) > 3 or len(mwdate) < 3:
        print('Error, date must be 3 characters in length.')
        windows()
    else:
        mwstart = input('Enter the time in 24h format for the beginning of the maintenance window (e.x. 04:00): ')
        mwend = input('Enter the ending time of the maintenance window in 24h format (e.x. 04:30): ')
        if int((mwstart and mwend).replace(':','')) < 1000 and (mwstart and mwend).startswith('0'):
            mwindow = mwdate.capitalize()+mwstart+'-'+mwdate.capitalize()+mwend
            return mwindow
        else:
            print('Error, be sure you prefix your window times with a 0 if they are earlier than 10:00.')
            windows()

print(windows())

I do not believe this is a duplicate, because all of the other questions have issues by the way of forgetting to pass the tested value back into the function, but in my case this does not ap

SamCyanide
  • 323
  • 4
  • 15

1 Answers1

5

You are ignoring the return value of recursive call, so your function just ends and return None. You could correct your windows() calls by using return windows() instead.

Better still, don't use recursion. Just use a loop and return when correct input has been given:

def windows():
    while True:
        print('\n---Maintenence Window Config---\n')
        mwdate = input('Enter the date for the maintenance window (3 letters, Mon, Tue, Wed, etc.) : ')
        if len(mwdate) > 3 or len(mwdate) < 3:
            print('Error, date must be 3 characters in length.')
            continue

        mwstart = input('Enter the time in 24h format for the beginning of the maintenance window (e.x. 04:00): ')
        mwend = input('Enter the ending time of the maintenance window in 24h format (e.x. 04:30): ')
        if int((mwstart and mwend).replace(':','')) < 1000 and (mwstart and mwend).startswith('0'):
            mwindow = mwdate.capitalize()+mwstart+'-'+mwdate.capitalize()+mwend
            return mwindow

        print('Error, be sure you prefix your window times with a 0 if they are earlier than 10:00.')

Also see Asking the user for input until they give a valid response

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Oh! I thought that when you issued a 'return' statement it would completely break the function. Thanks! I'll mark as answer as soon as the period is up :) – SamCyanide Jun 30 '15 at 14:17