โปรแกรมปฏิเสธที่จะเรียกใช้คำสั่ง 'if' แม้ว่าจะมีค่าที่ถูกต้องเป็นอินพุตก็ตาม

Aug 20 2020

ฉันใหม่มากกับการเขียนโปรแกรมคอมพิวเตอร์และกำลังเขียนโปรแกรมใน PyCharm Community ซึ่งเมื่อได้รับชื่อนักเรียนที่โรงเรียนของฉันจะพิมพ์คำแนะนำไปยังบ้านของนักเรียนคนดังกล่าวจากโรงเรียน

ทุกอย่างเป็นไปได้ด้วยดีและเมื่อคืนที่ผ่านมาฉันได้รับพื้นฐานของมัน วันนี้ฉันเปิดคอมพิวเตอร์ของฉันและด้วยเหตุผลบางประการโปรแกรมของฉันปฏิเสธที่จะเรียกใช้คำสั่ง 'if' / 'elif' ของฉันและจะเรียกใช้คำสั่ง else เท่านั้นแม้ว่าจะได้รับค่าที่ตรงตามคำสั่ง 'if' / 'elif' ก็ตาม

ฉันได้ลองเขียนโปรแกรมใหม่รีสตาร์ท PyCharm หลายครั้งตรวจสอบให้แน่ใจว่าฉันสอดคล้องกับช่องว่างและแท็บและตรวจสอบให้แน่ใจว่าตัวแปรของฉันทั้งหมดสามารถสื่อสารกันได้ ฉันขุดมาสักพักแล้วที่นี่และเว็บไซต์อื่น ๆ และฉันไม่เห็นเหตุผลว่าทำไมโค้ดของฉันถึงใช้งานได้เมื่อวานนี้ แต่ตอนนี้ปฏิเสธที่จะเรียกใช้อะไรเลยนอกจากคำสั่งอื่น

นี่คือรหัสของฉันซึ่งจะถามผู้ใช้ว่า "คุณต้องการไปที่ไหน" จากนั้นจะได้รับข้อมูล "บ้าน" เมื่อได้รับสิ่งนี้จะพิมพ์คำแนะนำออกมา แทนที่จะเรียกใช้คำสั่ง 'else' ทุกครั้ง

# Storing the names and directions of users:
David = "Directions to David's home from T... \n East on X, \n South on Y.," \
            " \n West on Z., \n South on A., \n first white house on the right."

Caroline = "Directions to Caroline's home from T... \n East on x, \n South on y.," \
        " \n East on z., \n South on p., \n East on q," \
        " \n West on t., \n last brick house in the cul-de-sac."

William = "Directions to Will's home from T... \n East on x, \n South on y.," \
        " \n West on z., \n South on Fa., \n West on b., \n first house on the right."

Bannon = "<Insert directions to Bannon's house>"

# User gives a specific name and then receives a location:
while True:
    destination = input("Where would you like to go? ")

    if destination.casefold() == 'Davids house':
        print(David)
        continue

    elif destination.casefold() == 'Carolines house':
        print(Caroline)
        continue

    elif destination.casefold() == 'Wills house':
        print(William)
        continue

    elif destination.casefold() == 'Bannons house':
        print(Bannon)
        continue

    # If an invalid location is given, this code will run:
    else:
        print("Sorry, that location wasn't found! Please try again.")
        continue

คำตอบ

2 mattbornski Aug 19 2020 at 23:50

casefold แปลงสตริงเป็นตัวพิมพ์เล็กและสตริงอ้างอิงของคุณมีอักขระตัวพิมพ์ใหญ่

ในการแก้ไขง่ายๆคุณสามารถเปลี่ยน "Davids house" เป็น "davids house" เป็นต้น

ในระยะยาวคุณอาจต้องการใช้การเปรียบเทียบที่เปราะบางน้อยกว่าเล็กน้อย แต่นั่นเป็นแบบฝึกหัดที่ยิ่งใหญ่และขึ้นอยู่กับว่าจะใช้โปรแกรมของคุณอย่างไรและผลที่ตามมาของความล้มเหลวในการแยกวิเคราะห์คืออะไร

2 marsnebulasoup Aug 20 2020 at 00:16

สำหรับการแก้ไขการพิมพ์ผิดและการสนับสนุนผู้ใช้ที่ทำสิ่งที่ทำให้การทดสอบผิดพลาดนี่คือตัวอย่างการใช้การเปรียบเทียบความคล้ายคลึงกันของสตริงเพื่อพิจารณาว่าอินพุตนั้นใกล้เคียงกับชื่อผู้ใช้หรือไม่:

import difflib
# Storing the names and directions of users: 
#This is called a dictionary. More info here https://www.w3schools.com/python/python_dictionaries.asp
directions= {
    "David": "Directions to David's home from T... \n East on X, \n South on Y.," \
            " \n West on Z., \n South on A., \n first white house on the right.",

    "Caroline": "Directions to Caroline's home from T... \n East on x, \n South on y.," \
        " \n East on z., \n South on p., \n East on q," \
        " \n West on t., \n last brick house in the cul-de-sac.",

    "William":"Directions to Will's home from T... \n East on x, \n South on y.," \
        " \n West on z., \n South on Fa., \n West on b., \n first house on the right.",

    "Bannon":"<Insert directions to Bannon's house>"
}

# User gives a specific name and then receives a location:
while True:
    destination = input("Where would you like to go? ")

    highest = 0 #highest score between the user name and input
    user_key = "" #name of the user who most closely matches the input
    for user in directions: #iterate through all the user's names in the directions dictionary
      similarity = difflib.SequenceMatcher( #for each user's name, compare it to the input
          None, destination, user).ratio()
      if(similarity > highest): #if the similarity score is greater than the highest recorded score, update the score
        highest = similarity
        user_key = user
    
    #Code that runs if a match is too low are not found
    if(highest < 0.5): #adjust this based on how close you want the match to be. highest will always be between 0.0 and 1.0
      print("Sorry, that location wasn't found! Please try again.")
      continue

    #Print user's directions
    else:
      print('\n\nGetting directions to ' + user_key + '\'s house\n\n')
      print(directions[user_key] + "\n\n\n")

ดังนั้นหากคุณเข้าไปใน "บ้านของวิลเลียม" "วิลเลียม" "บ้านของวิลเลียม" "วิลเลียม" หรืออะไรก็ตามที่อยู่ใกล้กับ "วิลเลียม" คุณจะได้รับเส้นทางไปยังบ้านของวิลเลียม

เรียกใช้ออนไลน์: https://repl.it/@marsnebulasoup/UprightMutedSymbol

1 tdelaney Aug 20 2020 at 00:06

ย่อโปรแกรมแล้วทดสอบ! คุณโพสต์รหัสมากกว่าที่จำเป็นเพื่อแสดงให้เห็นถึงปัญหา เมื่อคุณได้รับสิ่งที่เหมือนif destination.casefold() == 'Davids house':ไม่ได้ผลให้ย่อเล็กสุดให้เหลือเพียงปัญหาเดียวกับข้อมูลกระป๋อง

destination = "david's house"
if not destination.casefold() == "Davids house":
    print(repr(destination), "failed")

นี้พิมพ์

"david's house" failed

ความช่วยเหลือสำหรับcasefoldบอกว่าส่งคืนเวอร์ชันของสตริงที่เหมาะสำหรับการเปรียบเทียบแบบไม่ใช้ตัวพิมพ์เล็กและใหญ่ . อ๊ะนั่นสินะ คุณต้องพับทั้งสองด้าน แล้วก็มีเครื่องหมายวรรคตอนที่น่ารำคาญนั่น บางทีคุณอาจต้องการการทำให้เป็นมาตรฐานมากขึ้นเช่นการกำจัดอักขระที่ไม่ใช่ตัวอักษร

การย่อขนาดแสดงว่าคุณได้เขียนแบบทดสอบที่ดีสำหรับโค้ดของคุณ คุณสามารถเขียนฟังก์ชันเปรียบเทียบขนาดเล็กที่ทำ casefold และการทำให้เป็นมาตรฐานอื่น ๆ จากนั้นคุณสามารถเขียนการทดสอบเกี่ยวกับฟังก์ชันนั้นได้หลายสิบแบบเพื่อทดสอบกรณีขอบทั้งหมด