亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

盡管具有有效值作為輸入,程序仍拒絕運行“if”語句

盡管具有有效值作為輸入,程序仍拒絕運行“if”語句

子衿沉夜 2023-06-06 17:35:01
我是計算機編程的新手,目前正在 PyCharm Community 中編寫一個程序,當給出我學校學生的名字時,它會打印出從學校到該學生家的路線。一切都進行得很順利,昨晚我得到了它的基礎。今天我打開我的電腦,出于某種原因,我的程序拒絕運行我的“if”/“elif”語句,并且只會運行 else 語句,即使它的值滿足“if”/“elif”語句。我試過重寫程序,多次重啟 PyCharm,確保我的空格和制表符一致,并確保我的變量都可以相互通信。我已經在這里和其他網站上搜索了一段時間,但我只是看不出為什么我的代碼昨天能正常工作但現在除了 else 語句之外拒絕運行任何東西。這是我的代碼,它會詢問用戶“你想去哪里?” 然后將收到“房子”的輸入。一旦收到此信息,它將打印出他們的指示。取而代之的是,它每次都運行“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
查看完整描述

3 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

casefold將字符串轉換為小寫,并且您的參考字符串包含大寫字符。

作為一個簡單的修復,您可以將“Davids house”更改為“davids house”等。

從長遠來看,您可能希望實現稍微不那么脆弱的比較,但這是一項艱巨的任務,并且取決于您的程序將如何使用以及解析失敗的后果是什么。


查看完整回答
反對 回復 2023-06-06
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

對于拼寫錯誤糾正和支持用戶做破壞測試的事情,下面是一個使用字符串相似性比較來確定輸入是否接近任何用戶名的示例:


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


因此,如果您輸入“William's house”、“William”、“William's house”、“Williamm”或接近“William”的名稱,您將獲得前往 William 家的路線。

查看完整回答
反對 回復 2023-06-06
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

最小化程序并測試!您發布的代碼多于演示問題所需的代碼。一旦出現if destination.casefold() == 'Davids house':無法正常工作的情況,請盡量減少罐裝數據的問題


destination = "david's house"

if not destination.casefold() == "Davids house":

    print(repr(destination), "failed")

這打印


"david's house" failed

幫助casefold說Return a version of the string suitable for caseless comparisons. . 啊,就是這樣。你需要把兩邊都折疊起來。然后是那個討厭的撇號。也許您需要更多規范化,例如擺脫非字母字符。


通過最小化,您為代碼編寫了一個很好的測試。您可以編寫一個小的比較函數來執行 casefold 和其他規范化。然后,您可以對該函數編寫十幾個測試來測試所有邊緣情況。


查看完整回答
反對 回復 2023-06-06
  • 3 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號