Python 为什么我会收到;“不正确”;是否在此ifelse语句中输出?
我是python新手,试图通过做小项目来学习 我正试图编写一个程序,显示四个属性和属性的名称 要求用户识别不是铁路的属性。如果选择正确与否,应通知用户Python 为什么我会收到;“不正确”;是否在此ifelse语句中输出?,python,if-statement,conditional-statements,boolean-logic,Python,If Statement,Conditional Statements,Boolean Logic,我是python新手,试图通过做小项目来学习 我正试图编写一个程序,显示四个属性和属性的名称 要求用户识别不是铁路的属性。如果选择正确与否,应通知用户 properties = "Reading,","Pennsylvania","B & O","Short Line" question = str(input("Which is not a railroad?")) **Short Line** if properties == "Short Line": print("cor
properties = "Reading,","Pennsylvania","B & O","Short Line"
question = str(input("Which is not a railroad?")) **Short Line**
if properties == "Short Line":
print("correct")
else:
print("incorrect")
然而,我的最终输出显示为“不正确”,我做错了什么
铁路四大产业
在宾夕法尼亚州的雷丁,
B&O和短线。
哪一条不是铁路?短线
对的
短线是一家公共汽车公司。为您准备好了
print( "Reading, Pennsylvania, B & O, and Short Line. Which is not a railroad?" )
print("Which is not a railroad?")
answer = input()
if answer == "Short Line":
print("correct")
else:
print("incorrect")
我在你发布的代码中看到了一些东西 首先,不确定您的实际代码中是否有
**Short Line**
,但如果您试图注释,则在运行时不会对其进行解释
第二,正如其他答案中提到的,您正在检查数组中的属性。您应该对照问题中存储的输入进行检查
properties = "Reading,","Pennsylvania","B & O","Short Line"
question = str(input("Which is not a railroad?")) # **Short Line**
if question == "Short Line": # replaced properties with question
print("correct")
else:
print("incorrect")
print(properties)
print(question)
我发现,当我在理解某些东西为什么不起作用时,我会抛出一些print语句来查看变量在做什么。您可能希望在循环中捕捉用户,否则您必须不断运行代码才能找到正确答案(除非这是所需的行为,否则您可以保持原样)。此外,请注意,您可能希望使用大写或小写,因为用户可能会以“Short line”(小写字母“L”)的形式提供答案,而代码将返回为不正确。当然,这取决于您将接受什么作为答案 样品
print ("Reading,Pennsylvania,B & O, or Short Line. Which is not a railroad?")
user_input = input("Please provide an answer: ")
# != the loop will close once the user inputs short line in any form
# The upper.() will convert a user_input string to all caps
while user_input.upper() != "SHORT LINE":
print ("Incorrect, Please try again.")
user_input = input("Which one is not a railroad? ")
print ("Correct")
你可能想和一个IST
问题比较一下@KlausD.,贴出来作为答案,伙计!把那些选票拿上去。@Spenced有水果挂得太低了。对我来说不是太低了me@KlausD.,lol足够公平;-)让它更难看。是的,短线是输入。