How do I create a proper loop for my program to keep running until the user says "no"

Question:

january = "January has 31 days!"
february = "february has 28 days"
march = "march has 31 days"
april = "april has 30 days "
may = "may has 31 days"
june = "june has 30 days"
july = "july has 31 days"
august = "august has 31 days"
september = "september has 30 days"
october = "october has 31 days"
november = "november has 30 days"
december = "december has 31 days"

while True:
    ans = input("Do you want to know a specific months day count?")
    if ans == "yes":

      print("")
      you_input1 = input("Ok, Please type the months name:")
      break

    if ans == "no":
      time.sleep(1)
      print("")
      print("Ok, good bye!")
      break


if you_input1 == "january":
    print("")
    print(january)

if you_input1 == "february":
    print("")
    print(february)

if you_input1 == "march":
    print("")
    print(march)

if you_input1 == "april":
    print("")
    print(april)

if you_input1 == "may":
    print("")
    print(may)

if you_input1 == "june":
    print("")
    print(june)

if you_input1 == "july":
    print("")
    print(july)

if you_input1 == "august":
    print("")
    print(august)

if you_input1 == "september":
    print("")
    print(september)

if you_input1 == "october":
    print("")
    print(october)

if you_input1 == "november":
    print("")
    print(november)

if you_input1 == "december":
    print("")
    print(december)
    print("")

while True:
    print("")
    ans = input("view more?")
    if ans == "yes":
      print("")
      you_input1 = input("Please type a month name:")
      break
    if ans == "no":
      print("")
      print("Ok, good bye!")
      break

I am trying to create a program that when a user inputs a month, or the number of the month, (ie:February- 2nd month) the computer will output how many days that specific month has. what i want to do is, i want the user to be able to enter in as many months they want to until they say "no", then the computer is going to print "Ok, good bye!".

    if ans == "yes":
      print("")
      you_input1 = input("Please type a month name:")
      break
    if ans == "no":
      print("")
      print("Ok, good bye!")
      break

My issue is that the program is not looping. Whenever it asks me to enter a new month. and i input a new month, it just does not work anymore.
”’
you_input1 = input("Please type a month name:")
”’

Asked By: James Viray

||

Answers:

As stated in comments problem is that you have break.

Your code should like something like this:

while True:
    you_input1 = input("Please type a month name:")
    if ans == "No":
        break
    else:
        continue

Full code:

january = "January has 31 days!"
february = "february has 28 days"
march = "march has 31 days"
april = "april has 30 days "
may = "may has 31 days"
june = "june has 30 days"
july = "july has 31 days"
august = "august has 31 days"
september = "september has 30 days"
october = "october has 31 days"
november = "november has 30 days"
december = "december has 31 days"

while True:
    
    ans = input("Do you want to know a specific months day count?")
    if ans == "yes":
      print("")
      you_input1 = input("Ok, Please type the months name:")
      

    if ans == "no":
      time.sleep(1)
      print("")
      print("Ok, good bye!")
      break


    if you_input1 == "january":
        print("")
        print(january)
    
    if you_input1 == "february":
        print("")
        print(february)
    
    if you_input1 == "march":
        print("")
        print(march)
    
    if you_input1 == "april":
        print("")
        print(april)
    
    if you_input1 == "may":
        print("")
        print(may)
    
    if you_input1 == "june":
        print("")
        print(june)
    
    if you_input1 == "july":
        print("")
        print(july)
    
    if you_input1 == "august":
        print("")
        print(august)
    
    if you_input1 == "september":
        print("")
        print(september)
    
    if you_input1 == "october":
        print("")
        print(october)
    
    if you_input1 == "november":
        print("")
        print(november)
    
    if you_input1 == "december":
        print("")
        print(december)
        print("")
        
        print("")
        
    
        ans = input("view more?")
        if ans == "yes":
            print("")
        if ans == "no":
            print("")
            print("Ok, good bye!")
            break
        

Simple explanation:
2 while loops were simply too much in this case -> it’s enough to have one while True: -> if you’ve added it on last part of the program where the question was "view more", the program will end up constantly asking if you want to view more or not, it wouldn’t go back and ask for month and what not.
I’ve had to do indentation for last part, so it goes under first while loop.
This should make sense.

Also, this code is mess, there could be easier solution with functions:

from calendar import monthrange
import datetime
def checkQ(q):
    if q == "yes" or q == "Yes":
        return 1
    else:
        return 0

def check_month(month):
    if month == "january":
        return 1
    if month == "february":
        return 2
    if month == "march":
        return 3
    if month == "april":
        return 4
    if month =="may":
        return 5
    if month =="juny":
        return 6
    if month =="july":
        return 7
    if month =="august":
        return 8
    if month =="september":
        return 9
    if month =="october":
        return 10
    if month =="november":
        return 11
    if month =="december":
        return 12

def month(year,month):
    return monthrange(year, check_month(month))[0]
    
def question():
    while True:
        ans = input("Do you want to know a specific months day count?")
        if checkQ(ans):
            you_input1 = input("Ok, Please type the months name:")
            today = datetime.datetime.now()
            #accessing the year attribute
            year1 = today.year
            print(year1)
            print("There's " + str(monthrange(year1,month(year1,you_input1))[1]) + " days in [" + str(you_input1) + "].")
        else:
            print("Goodbye.")
        

question()
    

For sake of simplicity, I did this this way.

Answered By: str1ng
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.