程序拒绝运行'if'语句,尽管具有有效的值作为输入



我对计算机编程很陌生,目前正在PyCharm社区编写一个程序,当我的学校有一个学生的名字时,它会打印出从学校到该学生家的路线。

一切都进行得很顺利,我昨晚已经打好了基础。今天,我打开了我的计算机,由于某种原因,我的程序拒绝运行我的"if"/"lif"语句,并且只会运行else语句,即使它的值满足"if"或"lif"语句。

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

这是我的代码,它会询问用户";你想去哪里"然后将接收一个"0"的输入;"房子";。一旦收到这些信息,它就会打印出他们的指示。相反,它每次都运行'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

casefold将字符串转换为小写,并且引用字符串包含大写字符。

作为一个简单的解决方案,您可以将"大卫之家"更改为"大卫之屋"等。

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

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

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('nnGetting directions to ' + user_key + ''s housenn')
print(directions[user_key] + "nnn")

因此,如果你进入"威廉之家"、"威廉"、"威廉之家"或"威廉"附近的地方,你就会知道去威廉家的路。

在线运行:https://repl.it/@火星星云上行/上行静音符号

最小化程序和测试!您发布的代码超出了演示问题所需的数量。一旦你得到像if destination.casefold() == 'Davids house':这样的东西不起作用,就尽量减少罐装数据的问题

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

这将打印

"david's house" failed

casefold的帮助中说返回一个适用于无案例比较的字符串版本。啊,就是这样。你需要把两边都折叠起来。还有那个讨厌的撇号。也许你需要更多的规范化,比如去掉非字母字符。

通过最小化,您为代码编写了一个很好的测试。您可以编写一个小的compare函数来执行casefold和其他规范化操作。然后,您可以对该函数编写十几个测试来测试所有的边缘情况。

相关内容

最新更新