0

I'm a beginner of python I want to know how can I enter months continuously in my example

spring = [3, 4, 5]
summer = [6, 7, 8]
autumn = [9, 10, 11]
winter = [12, 1, 2]
month = int(input('please input month:'))
if month in spring:
    print(month, 'month is spring')
elif month in summer:
    print(month, 'month is summer')
elif month in autumn:
    print(month, 'month is autumn')
elif month in winter:
    print(month, 'month is winter')
else:
    print('Please input right month!')
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
rookie
  • 1
  • Tip: *while True* in conjunction with *break*. Also consider using *try* / *except* in case your user types something can't be interpreted as *int* –  Aug 29 '21 at 07:47

1 Answers1

1

You have to put your code inside an Infinite loop - while True:. Running the program indefinitely is a bad idea and so you should have an exit condition to stop execution.

You can have your own exit condition. This code will stop execution if user enters a -1.

spring = [3, 4, 5]
summer = [6, 7, 8]
autumn = [9, 10, 11]
winter = [12, 1, 2]

while True:
    month = int(input('please input month [Press -1 to exit]: '))
    if month == -1:
        print('Exiting...')
        break
    elif month in spring:
        print(month, 'month is spring')
    elif month in summer:
        print(month, 'month is summer')
    elif month in autumn:
        print(month, 'month is autumn')
    elif month in winter:
        print(month, 'month is winter')
    else:
        print('Please input right month!')
please input month [Press -1 to exit]: 5
5 month is spring
please input month [Press -1 to exit]: 2
2 month is winter
please input month [Press -1 to exit]: 100
Please input right month!
please input month [Press -1 to exit]: -1
Exiting...
Ram
  • 4,724
  • 2
  • 14
  • 22