Python while true 语句 - 不会将输入作为变量使用
Python while true statement - will not take the input as a variable to use
我正在尝试从任一选项中获取变量 start_date 和 end_date,以便稍后在我的程序中使用。但是代码也没有return。 start_date 和 end_date 的值仍然是之前输入的值,仍在内存中。任何想法如何解决?谢谢
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)
有几处需要更改
- 缩进 - while 循环在函数之外
- return
后无需中断
- 当一个函数是 returning 值时,需要在变量中捕获它们。
我已经进行了这些更改,希望这就是您所期待的
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))
输出
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
>
我正在尝试从任一选项中获取变量 start_date 和 end_date,以便稍后在我的程序中使用。但是代码也没有return。 start_date 和 end_date 的值仍然是之前输入的值,仍在内存中。任何想法如何解决?谢谢
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)
有几处需要更改
- 缩进 - while 循环在函数之外
- return 后无需中断
- 当一个函数是 returning 值时,需要在变量中捕获它们。
我已经进行了这些更改,希望这就是您所期待的
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))
输出
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
>