1

I've 2 questions with the following code:

positive = {'yes','Yes','y','Y'}
negative = {'no','No''n','N'}
month = March

while True:
    a = input('The month that you want to archive is March' '(Y or N) ? ')
    if a=='N':
        input("You will now leave, press Enter......")
        break
    elif a=='Y':
        print("Let's do it")
        input("The archive will start now, press Enter......")
        continue
    else:
        print("Enter either 'Y' to continue or 'N' to leave")

print (month)
etc....

Questions:

1 - How can I accept the input as one of positive variable options ('yes','Yes','y','Y') ?

2 - How can I have the month variable written in the input question instead of March hardcoded (as it is in this moment)?

3 - I don't know how to keep running the code when users users press "Y" then Enter. It keeps repeating the question instead of running the print(month) code.

Thank you very much in advance

  • 2
    Please fix your indentation. – cs95 Mar 16 '18 at 11:29
  • Use [formatstrings](https://docs.python.org/3/library/string.html#formatstrings) to include variable content into string output. – Patrick Artner Mar 16 '18 at 11:31
  • 1
    You should take a look at this question and answer: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). – mhawke Mar 16 '18 at 11:33

2 Answers2

0

That's one of the possible solutions (note that I am using list a list). If you want to support even more options without extending a list you might be interested in adding some RegExp.

positive = ['yes','Yes','y','Y']
negative = ['no','No', 'n','N']
month = "March"

while True:
    a = input('The month that you want to archive is March' '(Y or N) ? ')

    if a in negative:
        input("You will now leave, press Enter......")
        break
    elif a in positive:
        print("Let's do it")
        input("The archive will start now, press Enter......")
        break
    else:
       print("Enter either 'Y' to continue or 'N' to leave")

 print (month)
Edword
  • 80
  • 11
0

The below solution resolves several problems with your code.

positive = {'yes','Yes','y','Y'}
negative = {'no','No', 'n','N'}
month = 'March'

while True:
    a = input('The month that you want to archive is {0}: (Y or N)?'.format(month))

    if a in negative:
        input("You will now leave, press Enter......")
        break

    elif a in positive:
        print("Let's do it")
        input("The archive will start now, press Enter......")
        # perform your tasks here
        break

    else:
        print("Enter either 'Y' to continue or 'N' to leave")

Explanation

  • Test a versus the sets you have defined.
  • Use str.format to incorporate a variable in your input string.
  • Use break instead of continue to exit a loop after a positive value is entered; if you wish, you can include your "success" tasks inside the loop before break.
jpp
  • 159,742
  • 34
  • 281
  • 339