尽管输入有效值,程序仍拒绝运行“ if”语句

时间:2020-08-19 16:46:23

标签: python if-statement

我对计算机编程非常陌生,目前正在PyCharm社区中编写一个程序,当给我学校的一个学生的名字时,它将打印出从学校到该学生的住所的指示。

一切都进行得很好,昨晚我已经掌握了一切。今天,我打开计算机,由于某种原因,我的程序拒绝运行我的'if'/'elif'语句,并且即使给定一个满足'if'/'elif'语句的值,它也只会运行else语句。

我尝试重写程序,多次重新启动PyCharm,确保我与空格和制表符一致,并确保我的变量可以相互通信。我已经在这里和其他网站上搜索了一段时间,但我看不出为什么我的代码昨天能正常工作的原因,但现在除了else语句之外,其他都拒绝运行。

这是我的代码,它将询问用户“您想去哪里?”然后会收到“ house”的输入。收到此通知后,它将打印出他们的指示。取而代之的是,它每次都运行“ else”语句。

AzureDiagnostics
| where Category == "kube-audit"
| extend log_j=parse_json(log_s) 
| extend requestURI=log_j.requestURI 
| extend verb=log_j.verb 
| extend username=log_j.user.username
| extend requestObject = parse_json(log_j.requestObject)
| where verb !in ("get", "list", "watch", "")
| where username !in ("aksService", "masterclient", "nodeclient")
| where username !startswith "system:serviceaccount:kube-system"
| where requestURI startswith "/api/"
| where requestURI !startswith "/api/v1/nodes/"
| where requestURI !startswith "/api/v1/namespaces/kube-system/"
| where requestURI !startswith "/api/v1/namespaces/ingress-basic/"

3 个答案:

答案 0 :(得分:2)

casefold将字符串转换为小写,并且您的参考字符串包括大写字符。

作为一个简单的解决方法,您可以将“大卫之家”更改为“大卫之家”,等等。

从长远来看,您可能希望实现稍微不那么脆弱的比较,但这是一项艰巨的工作,取决于您将如何使用您的程序以及解析失败的后果。

答案 1 :(得分:2)

为纠正错字并支持用户做破坏测试的事情,下面是一个使用字符串相似性比较来确定输入是否接近任何用户名的示例:

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

答案 2 :(得分:1)

最小化程序并进行测试!您发布了过多的代码来演示此问题。一旦出现类似if destination.casefold() == 'Davids house':的问题,请尽量减少罐装数据的问题

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

此打印

"david's house" failed

casefold的帮助说返回适合无条件比较的字符串版本。。嗯,就是这样。您需要将两侧都折起来。然后就是那个讨厌的撇号。也许您需要更多规范化,例如摆脱非字母字符。

通过最小化,您为代码编写了一个很好的测试。您可以编写一个小的比较函数来执行大小写折叠和其他标准化操作。然后,您可以对该函数编写一打测试以测试所有边缘情况。