检查输入的字符串是否等于列表中的字符串?

时间:2017-12-04 23:59:36

标签: python string list function input

我正在开发一个项目,该项目涉及构建日历代理的简化版本,该日历代理会询问用户何时安排约会并为他们执行此操作(如果该插槽是免费的)。这是我到目前为止的代码:

def find_index(val, seq):
for index in range(len(seq)):
    place = seq[index]
    if place == val:
        return index
    else:
        return int("-1")

def find_val(val, seq):
    for ele in seq:
        if val == ele:
            return True
        else:
            return False

def init_nested_list(size_outer, size_inner):
    cal = []
    for outer_index in range(size_outer):
        nested_list = []
        for inner_index in range(size_inner):
            nested_list.append("-")
        cal.append(nested_list)
    return cal

def get_input(possible_vals, day_or_time_string):
    count = 0
    if day_or_time_string == "day":
        answer = input("What day would you like your appointment? ")
    else:
        answer = input("What time would you like your appointment? ")
    answer = answer.strip()
    nested_list = find_val(answer, possible_vals)
    while answer in possible_vals:
        break
    else:
        count = count + 1
        answer = input("Invalid entry. Please enter a valid day: ")
        if count == 3:
            print("This is getting silly - still not a valid entry")
            answer = input("Please do try to enter a valid day: ")
            count = 0
    return answer

def book_slot(cal,days_labels, times_labels, day, time): **ignore this function, haven't finished it yet**
    find_index(day, days_labels)

def start_scheduler(cal, days_labels, times_labels):
    while True:
        day = get_input(days_labels, "day")
        time = get_input(times_labels, "time")
        book_slot( cal, days_labels, times_labels, day, time)
        print("--------------------------------- ")
        res = input("Did you want to book more appointments (type n for no, any other key for yes)? ")
        if res == "n":
            break

days_labels= ["Monday","Tuesday","Wednesday","Thursday", "Friday"]
times_labels = ["9","10","11","12","1","2","3","4","5"]
calendar = init_nested_list(len(days_labels), len(times_labels))

print("Welcome to the acupuncture booking system. ")
start_scheduler(calendar, days_labels, times_labels)

到目前为止,这是完整输出的样子:

 Welcome to the acupuncture booking system. 
 What day would you like your appointment? saturday
 Invalid entry. Please enter a valid day: Monday
 What time would you like your appointment? 24
 Invalid entry. Please enter a valid time: 9
 --------------------------------- 
 Did you want to book more appointments (type n for no, any other key for yes)? 

然而,似乎无论我在函数询问约会的日期/时间时输入什么,它都不会检查输入的字符串是否等同于任何可接受的字符串(在列表中) days_labels和times标签)。相反,它只接受任何第二个随机输入是正确的,如下所示:

Welcome to the acupuncture booking system. 
What day would you like your appointment? s
Invalid entry. Please enter a valid day: z
What time would you like your appointment? d
Invalid entry. Please enter a valid day: f
--------------------------------- 
Did you want to book more appointments (type n for no, any other key for yes)? 

为了让用户“预约”约会,需要做什么才能让函数检查输入的字符串是否与days_labels和times_labels列表中的任何字符串相对应?

2 个答案:

答案 0 :(得分:0)

因此,您不会创建一个函数来检查是否已经使用了任何输入的字符串。 您的代码无法正常工作的原因是因为您尝试检查计数器最多为3的位置,而它不是任何循环,因此它只会上升到1。 例如,要以正确的方式重新安排,您可以这样做:

while answer not in possible_values:
<your code here>

答案 1 :(得分:-1)

我根本没有对此进行测试,但它足以引导您修复增量误差。

def isValidDayInput(input):
    accept = false
    # your code here
    return accept


def dayInput(count, maxAttempts):
    waiting = true
    while (waiting && count <= maxAttempts):
           answer = promptForInput()
           if (isValidDayInput(answer)): # accept returned true during validation
               waiting = false # answer is valid so jump out loop
           else(): # accept returned false during validation
               count += 1
   if (!waiting && count == maxAttempts):
          print("Too many incorrect attempts. Exit")
   else:
          print("thank you")