lundi 19 juin 2017

While loop printing a response when it shouldn't - Python

My program contains a simple 'Y/N' input. I have placed a while loop inside the code, which is supposed to respond when the input entered isn't 'Y/N' or any of it's variations.

import time
count_d = 10

load = input('Import modules? Y/N')

if load == 'Y' or load == 'y' or load == ' Y' or load == ' y':
    print('Modules loading...')
    while count_d >0:
        print(count_d)
        count_d = count_d-1
        time.sleep(1)
    print('Modules loaded')
elif load == 'N' or load == 'n' or load == ' N' or load == ' n':
    print('Shutdown initiated.')
    sys.exit()

while load != 'Y' or load != 'y' or load != ' Y' or load != ' y' and load != 'N' or load != 'n' or load != ' N' or load != ' n':
    print('This answer does not correspond with the desired output')
    load = input('Import modules? Y/N')
    if load == 'Y' or load == 'y' or load == ' Y' or load == ' y':
        print('Modules loading...')
        while count_d >0:
           print(count_d)
           count_d = count_d-1
           time.sleep(1)
        print('Modules loaded')
        break
    elif load == 'N' or load == 'n' or load == ' N' or load == ' n':
        print('Shutdown initiated.')
        break

When I do enter an accepted input, the program responds where it needs to, but also prints the response for the while loop, where it is designed to only recognise it with a unaccepted input. What errors have I made so it releases the wrong output when I have done it correctly?

Aucun commentaire:

Enregistrer un commentaire