2017-12-04 19 views
1

Ich arbeite an einem Projekt, bei dem eine vereinfachte Version eines Kalenderagenten erstellt wird, der den Benutzer fragt, wann er einen Termin planen möchte, und es für sie erledigt (wenn dieser Platz frei ist). Dies ist der Code habe ich bisher:Prüfen, ob eingegebene Strings gleich Strings in der Liste sind?

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) 

Dies ist, was die komplette Ausgabe wie bisher aussehen soll:

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)? 

Allerdings scheint es, dass, egal was ich eingegeben wird, wenn die Funktion fragt mich Für das Datum/die Uhrzeit des Termins prüft es nicht, ob die eingegebenen Zeichenfolgen mit den zulässigen übereinstimmen (in den Listen "Tage_Labels" und "Zeiten"). Stattdessen nimmt sie nur jede zweite Zufalls Eingabe korrekt zu sein, wie gezeigt:

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)? 

Was, um die Funktionskontrolle haben getan werden muss, um zu sehen, ob die eingegebenen Zeichenketten mit einer der Saiten in den days_labels und times_labels entsprechen Listen, damit der Benutzer einen Termin "buchen" kann?

Antwort

0

Sie werden also keine Funktion erstellen, um zu überprüfen, ob eine eingegebene Zeichenfolge bereits verwendet wurde. Der Grund, warum Ihr Code nicht richtig funktioniert, ist, weil Sie versucht haben, zu überprüfen, wo Ihr Zähler bis zu 3 ist, während es keine Schleife ist, und daher steigt er nur auf 1. Um das auf eine korrekte Weise zum Beispiel Sie neu anzuordnen würde das tun:

while answer not in possible_values: 
<your code here> 
-1

Ich habe das überhaupt nicht getestet, aber es sollte genug sein, um Sie zu führen, um Ihren steigenden Fehler zu beheben.

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") 
Verwandte Themen