首页 > 解决方案 > 如何在函数中正确使用while循环?

问题描述

我正在开发一个程序,如果用户收到,我需要使用 while 循环重新打开输入框Date not found, please try a different date

def ask_rain_date():

    date_rain_dict = seattle_weather.groupby('DATE')['RAIN'].apply(list).to_dict()

    rain_date_input = input("Seattle is always rainy! Check to see if it was raining on a date between 1948-2017!\n FORMAT  Y-M-D:  ")

    user_input = (rain_date_input)   

    if user_input in date_rain_dict:
        date_input = (date_rain_dict[user_input])
        if date_input == [True]:
            print('It was raining!')

        elif date_input == [False]:
            print('It was not raining!')


    else:
        print('Date not found, please try a different date.')

ask_rain_date()

我尝试while在 if 语句之外使用不同的变体,但它不起作用。我想可能会做类似的事情:while user_input <= True or user_input <= False就是这样。提前感谢所有帮助!

标签: pythonwhile-loopdataset

解决方案


def ask_rain_date():
    is_date_exists = False
    while not is_date_exists:
        dt = input()
        # searching logic
        if we_find_date_in_dataset:
            is_date_exists = True
            print('Is {}'.format(rain_status))
        else:
            print('Choose another day, please!')

类似的东西


推荐阅读