Herman L
Herman L

Reputation: 165

Python while true statement - will not take the input as a variable to use

I am trying to get from either choice the variables start_date and end_date to use later in my program. But the code does not return either. The values for start_date and end_date are still values entered ealrier and still in memory. Any ideas how to fix? Thanks.

def dateChoices():
    while True:
        print("Menu")
        print("1.All Dates")
        print("2.Selected Dates")
        print("3.Exit")
        choice=int(input("Enter your choice:"))


        if choice==1:
            start_date = ManagerAll['Min_Date'][0]  
            end_date = ManagerAll['Max_Date'][0]   
            print("All dates chosen" +str(start_date))
            return start_date, end_date
            break

        elif choice==2:
            start_date = input('enter a start date (format is YYYY-MM-DD):')
            start_date = pd.to_datetime(start_date, format='%Y-%m-%d')
            end_date = input('enter an end date (format is YYYY-MM-DD):')
            end_date = pd.to_datetime(end_date, format='%Y-%m-%d')
            print('start date chosen is' +str(start_date))
            print('end date chosen is' +str(end_date))
            return start_date, end_date
            break

        elif choice==3:
            break
        else:
            print("Wrong Choice")

    print(start_date)
    print(end_date)

    print(end_date)

Upvotes: 0

Views: 89

Answers (1)

Shreyas Prakash
Shreyas Prakash

Reputation: 614

Couple of things need to be changed

  1. Indentation - the while loop is outside of the function
  2. No need of break after return
  3. When a function is returning values, need to capture them in variables.

I have made these changes and hopefully this is what you are expecting

import pandas as pd

def dateChoices():
    while True:
        print("Menu")
        print("1.All Dates")
        print("2.Selected Dates")
        print("3.Exit")
        choice=int(input("Enter your choice:"))
    
        if choice==1:
            start_date = ManagerAll['Min_Date'][0]  
            end_date = ManagerAll['Max_Date'][0]   
            print("All dates chosen" +str(start_date))
            return start_date, end_date
    
        elif choice==2:
            start_date = input('enter a start date (format is YYYY-MM-DD):')
            start_date = pd.to_datetime(start_date, format='%Y-%m-%d')
            end_date = input('enter an end date (format is YYYY-MM-DD):')
            end_date = pd.to_datetime(end_date, format='%Y-%m-%d')
            return start_date, end_date
    
        elif choice==3:
            break
        else:
            print("Wrong Choice")
sd, ed = dateChoices() 
print('start date chosen is ' +str(sd))
print('end date chosen is ' +str(ed))

output

Menu
1.All Dates
2.Selected Dates
3.Exit
Enter your choice:2
enter a start date (format is YYYY-MM-DD):2001-04-15
enter an end date (format is YYYY-MM-DD):2002-09-09
start date chosen is 2001-04-15 00:00:00
end date chosen is 2002-09-09 00:00:00
> 

Upvotes: 1

Related Questions