2017-12-08 3 views
-1

私の機能の目標は、その日時のパラメータに対応するそのカレンダーのスロットを予約することです。予約は、カレンダーの値を「X」に変更することによって行われます(その日/時間が予約されていることを示すため)。ここで私が持っているコードは、これまでのところです:カレンダーのリストの値を変更するにはどうすればいいですか?

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

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 book_slot(cal,days_labels, times_labels, day, time): 
    pos1 = find_index(day, days_labels) 
    desired_day = cal[pos1] 
    pos2 = find_index(time, times_labels) 
    desired_time = desired_day[pos2] 
    if desired_day == "X": 
     print("We are sorry - that time is not available. Please try again.") 
    else: 
     print("Appointment is booked.") 


days_labels = ["Wednesday","Thursday","Friday"] 
times_labels = ["9","10","11"] 
cal = init_nested_list(len(days_labels), len(times_labels)) 
print("before" , cal) 
book_slot(cal, days_labels, times_labels, "Friday", "10") 
print("after" , cal) 

は、これは私が取得しています出力されます:

before [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']] 
Appointment is booked. 
after [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']] 

これは私が取得する必要があります出力されます。

before [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']] 
Appointment is booked. 
after [['-', '-', '-'], ['-', '-', '-'], ['-', 'X', '-']] 
We are sorry - that time is not available. Please try again. 

あなたのよう正しい出力は入れ子リストの2番目の要素(金曜日10時)を表示しますが、私のコードではそうではありません。私はdesired_dayは、ユーザーが予約したいと思っている日であることを知っていますが、それを適切に取得する方法は不明で、ユーザーが予約したときに文字列 "X"に割り当てます。私はまた、これを引き起こしているbook_slot関数にコーディングエラーがあることを知っていますが、もう一度、私はそれが何であるか確信しています...助けてください?

答えて

0

どこにもbook_slotXにはにはを割り当てますか。割り当てようとしているスロットを特定するのにpos1pos2を使うといいでしょう。

0

これは動作します。

def find_index(val, seq): 
    return seq.index(val) #Change 1 


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 book_slot(cal, days_labels, times_labels, day, time): 
    pos1 = find_index(day, days_labels) 
    desired_day = cal[pos1] 
    pos2 = find_index(time, times_labels) 
    if desired_day[pos2] is "X": # Change 2 
     print("We are sorry - that time is not available. Please try again.") 
    else: 
     print("Appointment is booked.") 
     desired_day[pos2] = "X" # Change 3 


days_labels = ["Wednesday", "Thursday", "Friday"] 
times_labels = ["9", "10", "11"] 
cal = init_nested_list(len(days_labels), len(times_labels)) 
print("before", cal) 
book_slot(cal, days_labels, times_labels, "Friday", "10") 
book_slot(cal, days_labels, times_labels, "Friday", "10") # Change 4: Need to do a booking twice to make it fail :-) 
print("after", cal) 
コードで 変更#ノートが表示されます出力:

('before', [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]) 
Appointment is booked. 
('after', [['-', '-', '-'], ['-', '-', '-'], ['-', 'X', '-']]) 
We are sorry - that time is not available. Please try again. 
関連する問題